ProgrammingHardWipro · Cognizant technical pattern
What is printed?
What is printed?
String s = "b";
switch (s) {
case "a": System.out.print("A");
case "b": System.out.print("B");
case "c": System.out.print("C"); break;
default: System.out.print("D");
}- A. B
- B. BCD
- C. D
- D. BC
Correct answer: D. BC
The switch jumps to case "b" and, with no break, falls through into case "c", printing C, then stops at its break. default is never reached.
More OOP with C++ and Java questions
- What happens when this Java code is compiled? java try { FileReader r = new FileReader("a.txt"); } catch (Exception e) {…
- This class is meant to be immutable. What change is still needed? java public final class Team { private final List<String> members…
- What is printed? cpp struct V { int x; V(int a) : x(a) {} V operator+(const V& o) const { return V(x + o.x 2); } }; int main() { V a(1)…
- What is printed? java class A { void m(A a) { System.out.print("AA"); } } class B extends A { void m(A a) { System.out.print("BA"); } void…
- Class P is in package a and declares protected int x. Class C is in package b and extends P. Inside an instance method of C, which of…
- What happens when this Java class is compiled? java class Util { static void f(List<String> l) { } static void f(List<Integer> l) { } }