ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement
Which line fails to compile?
Which line fails to compile?
byte b = 10; b += 5; // line 1 b++; // line 2 b = b + 1; // line 3 b = 10 + 20; // line 4
- A. Line 1
- B. Line 3
- C. Line 4
- D. Lines 1 and 3
Correct answer: B. Line 3
b + 1 is an int expression and needs an explicit cast to go back into a byte. Compound assignment and ++ include an implicit cast, and 10 + 20 is a constant 30 that fits in a byte, so it narrows automatically.
More OOP with C++ and Java questions
- What is printed? java int i = 0, j = 0; boolean p = (i++ > 0) & (i++ > 0); boolean q = (j++ > 0) && (j++ > 0); System.out.print(i + " " +…
- What is printed? java class P { private void f() { System.out.print("P"); } void call() { f(); } } class C extends P { void f() {…
- What is printed? java class P { static String s() { return "S"; } } // in main: P p = null; System.out.print(p.s());
- Which line fails to compile? java interface I { static String s() { return "I"; } default String d() { return "D"; } } class C implements…
- What is printed? java int[] a = {1, 2}; int[] b = {1, 2}; System.out.print(a.equals(b) + " " + java.util.Arrays.equals(a, b) + " " +…
- What is printed? java outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == i) continue outer; if (i + j == 3) break…