You are on page 1of 7

13. Which one of the following statements are false?

A. All Java operators are subject to arithmetic promotions


B. A reference to a primitive array can be checked with the instanceof operator
C. Relational operators would result non boolean values
D. Logical operators would result non Boolean values
E. All these statements are true

Answer : B,C,D,E
A.Cosidering the first statement; it should be correct. If we write a code like this,

Here we have put an arithmetic


operator. What will happen regarding
bitwise operators,

(a+b) gives a integer answer in the first example, which


we trying to assign to a byte variable; so the whole thing
leads to an error. This error comes because byte variable
b is “Promoted ” to a integer before the calculation takes
place and it provides evidence for “arithmetic promotion”

The most important thing comes in the second situation.


There I have put a “bitwise operator” and result is the same. Thus I can justify my answer that, all java
operators are subjected to arithmetic promotions.

B. The “Instance Of” operator is used in determining, if a given object is of the type of a specific class.It is
a binary operator, first operand sholud be an object and second operand should be a class name. The
manipulation should be like follows,
(object instanceOf class name)
Here it says that a reference to a primitive array can also be checked by, using instanceof operator.
Primitive array consisting primitive data type variables, can’t have a connection with objects. So I
assume that this statement might be wrong.

C. This statement is wrong because relational operators compare two operands and always return either
true or false.
D.Logical operators and Bitwise Operatos are really tricy, because they look like almost same, if you give
two boolean operands it’ll behave like a logical operator and return a boolean value and, if the operands
are numerical, operator ‘ll act like in bitwise manner and result a numerical answer. But here in this
scenario, clearly question says that logical operator is used, so the answer should be a boolean. And
there’s no way to result non boolean value and that relization make it confirmed; that statement D is
wrong.

14. Consider the following code

class A{
public static void main (String args []){
System.out.println(5+2 == “” + 5 + 2 );
}
}
What is the result?
A . true
B.false
C.No output
D.Compilation Error
E. Runtime Exception
Answer : D
Before explaining the answer, I would like to show you some examples, which will help to understand
the situation. The following code compiles fine, gives shown out put,

So does the second code,

But the code in the question, won’t compile fine,

Compiler consider one side of the == as a String and the other side as an integer, thus can’t be campare
15. Consider the following code

class A {
public static void main (String args[]){
boolean a = false;
boolean b = !a;
boolean c = (a^ !b);
System.out.println(a| b ^ c & ! a );
System.out.println(((a | b ) ^ c ) & ! a );
}
}
What is the result?
A. True true
B. False false
C. True false
D. False true
E. Compilation Error
F. Runtime Exception
Answer : A

When looking at the code, wwe can recognize various logical operators which have been used. Not, OR ,
Ex-OR and AND operators are among them. If you carefully examine the code, you can understand that
the result is true in both situations.

Output :

16. Consider the following code

Class A {
public static void main (String args[]){
int x=10;
float y=30.0F;
long z=x*y;
double d = (x<y) ? z : (y<x) ? y : x;
System.out.println(d);
}
}
What is the result ?
A. 10.0
B. 30.0
C. 300.0
D. 300
E. Compilation Error
F. Runtime Exception
Answer : E

If you try to compile the preceding code, you’ll met up with a compile error.

To perform the (x*y) operation,


bothe x and y will be promoted to float, according to the “arithmatic promotion”. Now most probably,
we are likely to get 300.0 from the calculation (10*30.0). Then we try to assign the value to a long
variable. Although the long data typr is 64 bit and larger than float, compiler warns by saying “a data loss
might happen !” because obviosly long variable can’t take the decimal part of the float value.
17.What is the result of following expressions?
a. 17%5
b. -5%2
c. 7.6%2.9

1. 7,1,1.8
2. 2,-1,1.0
3. 2,1,1.8
4. None of the above

Answer : 4
We can write a simple java program to justify the answer.

s 2

a -1

x 1.8

First two calculations are simple and you just need to know the basic behaviour of modulus operator.
And the final one is regarding numbers with decimal points and the answer I assumed can be proved
through java coding in the above way.

18. class V{
Public static void main(String args[]){
System.out.print((0.0%-1)+”,”+(-0.0%1)+”,”+(-0.0%-1));

}
}

What is the result of attempting to compile and run the program?


a. Prints: 0.0,0.0,0.0
b. Prints: 0.0,-0.0,-0.0
c. Prints:- 0.0,-0.0,0.0
d. Prints: 0.0,0.0,-0.0
e. Prints:- 0.0,0.0,0.0
f. Prints:- 0.0,-0.0,-0.0
g. Prints: 0.0,-0.0,0.0
h. Prints:- 0.0,0.0,-0.0
i. None of the above

Answer : b

Although the answers of the question cotain very similar answers, and looks scary, the question isn’t so
difficult.

Good knowledge about different situations where the % operator can be used with (-) numbers, is
important, to get exactly the correct answer.

You might also like