JAVA NOTES FOR Class VIII
JAVA NOTES FOR Class VIII
CHAPTER 1
OBJECT ORIENTED PROGRAMMING
1
9. What is the difference between an object and a class? Ans. 1. A class is a
blueprint or a prototype of a real world object. It contains instance variables and
methods whereas an object is an instance of a class.
2. A class exists in the memory of a computer while an object does not.
3. There will be only one copy of a class whereas multiple objects can be instantiated
from the same class.
10. Why is a class called a factory of objects? Ans. A class is known as a factory of
objects because objects are instantiated from classes. Each object gets a copy of the
instance variables present in the class. It is like a factory producing objects.
11. Why is a class known as a composite data type? Ans. A class is composed of
instance variables which are of different data types. hence, a class can be viewed as a
composite data type which is composed of primitive and other composite data types.
Chapter 2
Introduction to Java
2
4. The smallest individual unit in a program is a token.
5. Identifiers are fundamental building blocks that give names to different parts
of a program.
6. Escape sequences/Non graphic characters are characters that cannot be typed
directly from the keyboard.
7. Operators that act upon two operands are referred to as Binary operators.
8. Variables represent named storage locations whose values can be
manipulated during program execution.
9. The new operator is used to allocate memory for objects and arrays.
3
Range
Size in Default (Smallest and
Primitive Data Type Bytes Value Biggest Value)
byte 1 0 -128 to 127
short 2 0 -32768 to 32767
Integer
int 4 0 −231 to 231−1
Long 8 0L -263 to 263−1
float 4 0.0f - 3.4E+38 to 3.8
Real -1.7E+308 to
double 8 0.0d 1.7E+308
char 2 \u0000 0 to 65535
boolean 1 bit FALSE
11. What do you mean by operator and write the name of all operators given in your
textbook. Ans: Operator – are the entities or symbols that tell the compiler that what
operation has to be performed with one, two or three number of operands within a
given expression.
There are approximately a set of 37 different operators that Java uses
Operators can either operate on 1, 2 or 3 operands and accordingly named as –
Unary (works on 1 operand) e.g. + + and – –
Binary (works on 2 operands) e.g. +, – , * , / , >, <, == etc…
Ternary (works on 3 operands) e.g. ? :
12. Categorize the different types of Operators. Ans The different types of Operators are
Arithmetic Operators (+ , – , * , / , %)
Relational Operators (> , < , >= , <= , == , !=)
Logical Operators (&&, || , !)
Conditional Operators (? : )
are the entities or symbols that tell the compiler that what operation has to be
performed with one, two or three number of operands within a given expression.
13. Explain Maths functions in Java
The Math class belongs to the default package of Java named java.lang)
Math.min(a, b) Returns the smaller among a and b
Math.max(a, b) Returns the larger among a and b
Math.abs(a) Convert -ve magnitude to +ve magnitude.
Math.sqrt(x) Returns the square root of ‘x’
Math.pow(x, y) Returns x to the power of y
Math.cbrt(x) Returns the cube root of ‘x’
Math.floor(x) Returns the integral value of x less than or equal to ‘x’ but datatype
returned is double, hence Math.floor(7.9) will give 7.0
Math.ceil(x) Returns the integral value of x greater than or equal to ‘x’ but datatype
returned is double, hence Math.ceil(7.9) will give 8.0
14. Explain the methods print() and println()?Ans Java supports two output methods
that can be used to send the results to the screen. print() method and println()
method.The print() method prints output on one line. The println() method by contrast
takes the information provided and displays it on a line followed by a line feed.
4
1. a) Which of the following are valid comments?
(i) /* comment */ (ii) /*comment (iii) //comment (iv) */ comment */
Ans: i. and iii.
2. State the two kinds of data types.
Ans: The two types of data types are: Primitive and non-primitive/composite/user define
data types. The primitive data types are: byte, short, int, long, float, double, char and
Boolean. The non-primitive/reference data types are: class, array and interface.
3. Assign the value of pi(0.3174) to a variable with requisite data type
Ans double pi= 0.3174;
4. Name the primitive data type in Java that is:
(i) a 64-bit integer and is used when you need a range of values wider than those
provided by int.-- long
(ii) a single 16-bit Unicode character whose default value is ‘\u0000′ → char
5. Give one example each of a primitive data type and a composite data type.
Ans. Primitive Data Types – byte, short, int, long, float, double, char, boolean
Composite Data Type – Class, Array, Interface
6. Classify the following as primitive or non-primitive data types:
(i) char (ii) arrays (iii) int (iv) classes
Ans. (i) char – Primitive (ii) arrays – Non primitive
(iii) int – Primitive (iv) Classes – Non primitive
7. Arrange the following primitive data types in an ascending order of their size:
(i) char (ii) byte (iii) double (iv) int
Ans. The sizes of the given data types are as follows
char = 2 bytes byte = 1 byte double = 8 bytes int = 4 bytes
Arranging them in ascending order gives byte < char < int < double
8. What is the default of int and float ? Ans int –>0 and float 0.0f
9. State the number of bytes occupied by char and int data types.
Ans. char occupies two bytes and int occupied 4 bytes.
10. State the Java concept that is implemented through:
i) a super class and a subclass. – Inheritance
ii) the act of representing essential features without including background details.
Abstraction
11. Identify the literals listed below : (i)0.5 (ii)’A’ (iii)false (iv)”a”
i. double literal ii. Character literal iii. Boolean literal iv. String literal
12. Identify the following tokens
i. public ii. ‘a’ iii. == iv. { }.
Ans. public → Keyword ‘a’ → Literal == → Operator {} → Separator
13. Write a Java expression for the following:
√(3x + x2) / (a + b) Ans. Math.sqrt(3 * x + Math.pow(x, 2)) / (a + b)
2
|x + 2xy| Ans Math.abs(x * x + 2 * x * y)
5 3
c. ax + bx + c Ans. a * Math.pow(x, 5) + b * Math.pow(x, 3) + c
2
√𝐴 + 𝐵 + 𝐶2 2
5
15. Give Output i) Math.ceil(4.2) → 5.0
(ii) Math.abs(-4) ➔ 4
16. Give Output of the following functions.
a) Math.floor(–26.349)→-27.0
b) Math.floor(15.36) →15.0
c) Math.min(–0.0,0.0) -→0.0
d) Math.pow(2,3) →8.0
e) Math.sqrt(625) →25.0
f) Math.cbrt(125)→5.0
g) Math.ceil(–14.0) = -14.0
i) Math.ceil(–12.56) -12.0
j) Math.max(4.6,1.3) 4.6
l) Math.max(11,11) 11
m) Math.min(14.3,14.3) 14.3
17. What is the value of y after evaluating the expression given below?
i. y += ++y + y– + –y; when int y = 8.
Ans. y += ++y + y– + –y y = 8 + ++y + y– + –y
y=8+9+9+7 y = 33