Question 1
(a) Name any two basic principles of Object-oriented Programming.
Answer
1. Encapsulation
2. Inheritance
(b) Write a difference between unary and binary operator.
Answer
unary operators operate on a single operand whereas binary operators operate on two operands.
Question 2
Write the memory capacity (storage size) of short and float data type in bytes.
Answer
1. short — 2 bytes
2. float — 4 bytes
Question 3
Identify and name the following tokens:
1. public
2. 'a'
3. ==
4. {}
Answer
1. Keyword
2. Literal
3. Operator
4Separator
Question 4
What are the various types of errors in Java?
Answer
There are 3 types of errors in Java:
1. Syntax Error
2. Runtime Error
3. Logical Error
Question 5
(a) Write a Java expression for the following:
|x2 + 2xy|
Answer
Math.abs(x * x + 2 * x * y)
Question 6
If the value of basic=1500, what will be the value of tax after the following statement is executed?
tax = basic > 1200 ? 200 :100;
Answer
Value of tax will be 200. As basic is 1500, the condition of ternary operator — basic > 1200 is true. 200 is
returned as the result of ternary operator and it gets assigned to tax.
Question 7
Give the output of the following:
Math.sqrt(Math.max(9,16))
Answer
The output is 4.0.
First Math.max(9,16) is evaluated. It returns 16, the greater of its arguments. Math.sqrt(Math.max(9,16))
becomes Math.sqrt(16). It returns the square root of 16 which is 4.0.
Question 8
Evaluate the following expression if the value of x=2, y=3 and z=1.
v=x + --z + y++ + y
Answer
⇒v=2+0+3+4
v = x + --z + y++ + y
⇒v=9
Question 9
What is meant by a package? Give an example.
Answer
A package is a named collection of Java classes that are grouped on the basis of their functionality. For example,
java.util.
Question 10
Classify the following as primitive or non-primitive datatypes:
1. char
2. arrays
3. int
4. classes
Answer
1. Primitive
2. Non-Primitive
3. Primitive
4. Non-Primitive
(a) (i) int res = 'A';
What is the value of res?
Answer
Value of res is 65 which is the ASCII code of A. As res is an int variable and we are trying to assign it the
character A so through implicit type conversion the ASCII code of A is assigned to res.
(b) What is the value of y after evaluating the expression given below?
y+= ++y + y-- + --y; when int y=8
Answer
⇒ y = y + (++y + y-- + --y)
y+= ++y + y-- + --y
⇒ y = 8 + (9 + 9 + 7)
⇒ y = 8 + 25
⇒ y = 33
(c) Give the output of the following:
1. Math.floor (-4.7)
2. Math.ceil(3.4) + Math.pow(2, 3)
Answer
1. -5.0
2. 12.0
3. (i) Rewrite the following using ternary operator:
4. if (bill > 10000 )
5. discount = bill * 10.0/100;
6. else
7. discount = bill * 5.0/100;
8. Answer
9. discount = bill > 10000 ? bill * 10.0/100 : bill * 5.0/100;
(a) What is inheritance?
Answer
Inheritance is the mechanism by which a class acquires the properties and methods of another class.
(b) Name the operators listed below:
1. <
2. ++
3. &&
4. ?:
Answer
1. Less than operator. (It is a relational operator)
2. Increment operator. (It is an arithmetic operator)
3. Logical AND operator. (It is a logical operator)
4. Ternary operator. (It is a conditional operator)
(c) State the number of bytes occupied by char and int data types.
Answer
char occupies 2 bytes and int occupies 4 bytes.
(d) Write one difference between / and % operator.
Answer
/ operator computes the quotient whereas % operator computes the remainder.
(e) Write the output:
char ch = 'F';
int m = ch;
m=m+5;
System.out.println(m + " " + ch);
Answer
Output of the above code is:
75 F
Explanation
The statement int m = ch; assigns the ASCII value of F (which is 70) to variable m. Adding 5 to m makes it
75. In the println statement, the current value of m which is 75 is printed followed by space and then value of ch
which is F.
(a) Write a Java expression for the following:
ax5 + bx3 + c
Answer
a * Math.pow(x, 5) + b * Math.pow(x, 3) + c
(b) What is the value of x1 if x=5?
x1= ++x – x++ + --x
Answer
⇒ x1 = 6 - 6 + 6
x1 = ++x – x++ + --x
⇒ x1 = 0 + 6
⇒ x1 = 6
(c) Why is an object called an instance of a class?
Answer
A class can create objects of itself with different characteristics and common behaviour. So, we can say that an
Object represents a specific state of the class. For these reasons, an Object is called an Instance of a Class.
(d) Convert following do-while loop into for loop.
int i = 1;
int d = 5;
do {
d=d*2;
System.out.println(d);
i++ ; } while ( i<=5);
Answer
int i = 1;
int d = 5;
for (i = 1; i <= 5; i++) {
d=d*2;
System.out.println(d);
}
(g) What are the values stored in variables r1 and r2:
1. double r1 = Math.abs(Math.min(-2.83, -5.83));
2. double r2 = Math.sqrt(Math.floor(16.3));
Answer
1. r1 has 5.83
2. r2 has 4.0
Explanation
1. Math.min(-2.83, -5.83) returns -5.83 as -5.83 is less than -2.83. (Note that these are negative numbers).
Math.abs(-5.83) returns 5.83.
2. Math.floor(16.3) returns 16.0. Math.sqrt(16.0) gives square root of 16.0 which is 4.0.
3. What is the difference between the Scanner class functions next() and nextLine()?
4. Answer
next() nextLine()
It reads the input only till space so it can read only a It reads the input till the end of line so it can
single word. read a full sentence including spaces.
It places the cursor in the same line after reading the It places the cursor in the next line after
input. reading the input.
2019-17