Sail E0 Webinar

MCQs

Total Questions : 65 | Page 4 of 7 pages
Question 31.
Which of these lines of code will give better performance?
1. a | 4 + c >> b & 7;
2. (a | ((( 4 * c ) >> b ) & 7 ))
  1.    1 will give better performance as it has no parentheses.
  2.    2 will give better performance as it has parentheses.
  3.    Both 1 & 2 will give equal performance.
  4.    Dependent on the computer system.
 Discuss Question
Answer: Option C. -> Both 1 & 2 will give equal performance.

Parentheses do not degrade the performance of the program. Adding
parentheses to reduce ambiguity

does not negatively affect your system.


Question 32.


What will be the output of the program?


class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) || (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
  1.    5 3
  2.    8 2
  3.    8 3
  4.    8 5
 Discuss Question
Answer: Option B. -> 8 2

The first two iterations of the for loop both x and y are incremented. On the third iteration 

x is incremented, and for the first time becomes greater than 2. The short circuit or operator 

|| keeps y from ever being incremented again and x is incremented twice on each of the last 

three iterations.


Question 33.


What will be the output of the program?


class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) && (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
  1.    5 2
  2.    5 3
  3.    6 3
  4.    6 4
 Discuss Question
Answer: Option C. -> 6 3

In the first two iterations x is incremented once and y is not because of the short circuit && 

operator. In the third and forth iterations x and y are each incremented, and in the fifth iteration 

x is doubly incremented and y is incremented.


Question 34.


What is the output of this program?


class leftshift_operator {
public static void main(String args[])
{
byte x = 64;
int i;
byte y;
i = x
  1.    0 64
  2.    64 0
  3.    0 256
  4.    256 0
 Discuss Question
Answer: Option D. -> 256 0

None.
output:
$ javac leftshift_operator.java
$ java leftshift_operator
256 0


Question 35.


What is the output of this program?


class ternary_operator {
public static void main(String args[])
{
int x = 3;
int y = ~ x;
int z;
z = x > y ? x : y;
System.out.print(z);
}
}
  1.    0
  2.    1
  3.    3
  4.    -4
 Discuss Question
Answer: Option C. -> 3

None.
output:
$ javac ternary_operator.java
$ java ternary_operator
3


Question 36.


What will be the output of the program?


class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) || (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
  1.    5 3
  2.    8 2
  3.    8 3
  4.    8 5
 Discuss Question
Answer: Option B. -> 8 2

The first two iterations of the for loop both x and y are incremented. On the third iteration 

x is incremented, and for the first time becomes greater than 2. The short circuit or operator

 || keeps y from ever being incremented again and x is incremented twice on each of the last 

three iterations.


Question 37.


What is the output of this program?


class increment {
public static void main(String args[])
{
int g = 3;
System.out.print(++g * 8);
}
}
  1.    25
  2.    24
  3.    32
  4.    33
 Discuss Question
Answer: Option C. -> 32

Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.
output:
$ javac increment.java
$ java increment
32


Question 38.


What is the output of this program?


class bitwise_operator {
public static void main(String args[])
{
int a = 3;
int b = 6;
int c = a | b;
int d = a & b;
System.out.println(c + " " + d);
}
}
  1.    7 2
  2.    7 7
  3.    7 5
  4.    5 2
 Discuss Question
Answer: Option A. -> 7 2

And operator produces 1 bit if both operand are 1. Or operator produces 1 bit if any bit of the two

operands in 1.
output:
$ javac bitwise_operator.java
$ java bitwise_operator
7 2


Question 39.


What will be the output of the program?


class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) && (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
  1.    5 2
  2.    5 3
  3.    6 3
  4.    6 4
 Discuss Question
Answer: Option C. -> 6 3

In the first two iterations x is incremented once and y is not because of the short circuit && 

operator. In the third and forth iterations x and y are each incremented, and in the fifth iteration 

x is doubly incremented and y is incremented.


Question 40.


What is the output of this program?


class Modulus {
public static void main(String args[])
{
double a = 25.64;
int b = 25;
a = a % 10;
b = b % 10;
System.out.println(a + " " + b);
}
}
  1.    5.640000000000001 5
  2.    5.640000000000001 5.0
  3.    5 5
  4.    5 5.640000000000001
 Discuss Question
Answer: Option A. -> 5.640000000000001 5

Modulus operator returns the remainder of a division operation on the
operand. a = a % 10 returns
25.64 % 10 i:e 5.640000000000001. Similarly b
= b % 10 returns 5.
output:
$ javac Modulus.java
$ java Modulus
5.640000000000001 5


Latest Videos

Latest Test Papers