Operators and Assignments Questions and Answers

Practice Mode
Showing 10 of 38 questions
Q11
What will be the output of the program? class SC2 {     public static void main(String [] args)     {         SC2 s = new SC2();         s.start();     }     void start()     {         int a = 3;         int b = 4;         System.out.print(" " + 7 + 2 + " ");         System.out.print(a + b);         System.out.print(" " + a + b + " ");         System.out.print(foo() + a + b + " ");         System.out.println(a + b + foo());     }     String foo()     {         return "foo";     } }
  • A 9 7 7 foo 7 7foo
  • B 72 34 34 foo34 34foo
  • C 9 7 7 foo34 34foo
  • D 72 7 34 foo34 7foo
Answer: Option D
Q12
What will be the output of the program? class Test {     static int s;     public static void main(String [] args)     {         Test p = new Test();         p.start();         System.out.println(s);     }     void start()     {         int x = 7;         twice(x);         System.out.print(x + " ");     }     void twice(int x)     {         x = x*2;         s = x;     } }
  • A 7 7
  • B 7 14
  • C 14 0
  • D 14 14
Answer: Option B
Q13
What will be the output of the program? class Two {     byte x; } class PassO {     public static void main(String [] args)     {         PassO p = new PassO();         p.start();     }     void start()     {         Two t = new Two();         System.out.print(t.x + " ");         Two t2 = fix(t);         System.out.println(t.x + " " + t2.x);     }     Two fix(Two tt)     {         tt.x = 42;         return tt;     } }
  • A null null 42
  • B 0 0 42
  • C 0 42 42
  • D 0 0 0
Answer: Option C
Q14
What will be the output of the program? class BoolArray {     boolean [] b = new boolean[3];     int count = 0;     void set(boolean [] x, int i)     {         x[i] = true;         ++count;     }     public static void main(String [] args)     {         BoolArray ba = new BoolArray();         ba.set(ba.b, 0);         ba.set(ba.b, 2);         ba.test();     }     void test()     {         if ( b[0] && b[1] | b[2] )             count++;         if ( b[1] && b[(++count - 2)] )             count += 7;         System.out.println("count = " + count);     } }
  • A count = 0
  • B count = 2
  • C count = 3
  • D count = 4
Answer: Option C
Q15
What will be the output of the program? public class Test {     public static void leftshift(int i, int j)     {         i <<= j;     }     public static void main(String args[])     {         int i = 4, j = 2;         leftshift(i, j);         System.out.printIn(i);     } }
  • A 2
  • B 4
  • C 8
  • D 16
Answer: Option B
Q16
What is the output of int a = 5; a += 3 * 2;?
  • A 11
  • B 16
  • C 13
  • D 10
Answer: Option A
Explanation: a += 3 * 2 -> a = 5 + 6 = 11.
Q17
Which operator has the highest precedence?
  • A +
  • B ++
  • C &&
  • D =
Answer: Option B
Explanation: Unary operators like ++ have higher precedence than arithmetic and logical operators.
Q18
What is the result of 10 / 3 in Java (integers)?
  • A 3.3
  • B 3
  • C 4
  • D 0
Answer: Option B
Explanation: Integer division truncates the decimal part.
Q19
int x = 7; int y = ++x * 2; What is y?
  • A 14
  • B 16
  • C 15
  • D 12
Answer: Option B
Explanation: ++x makes x = 8, so y = 8 x 2 = 16.
Q20
What is the output of System.out.println(5 + 3 + "7");?
  • A 57
  • B 157
  • C 87
  • D 87.0
Answer: Option C
Explanation: Left side evaluated first: 5+3 = 8, then "8" + "7" = "87".
Questions and Answers for Competitive Exams Various Entrance Test