ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
interface Shape { double area(); }
class Sq implements Shape {
double area() { return 4; }
}
// in main:
Shape s = new Sq();
System.out.print(s.area());- A. 4.0
- B. 4
- C. A runtime AbstractMethodError
- D. Compile-time error: area() in Sq has weaker access than in Shape
Correct answer: D. Compile-time error: area() in Sq has weaker access than in Shape
Interface methods are implicitly public, and an overriding method cannot reduce visibility. Sq.area() is package-private, so the class does not compile; it must be declared public.
More OOP with C++ and Java questions
- Which declaration is LEGAL inside an abstract Java class?
- Which line fails to compile? java class Outer { int x = 1; class In { int get() { return x; } } static class SN { int get() { return 2; }…
- What is printed? java class Outer { int x = 1; class Inner { int x = 2; void show(int x) { System.out.print(x + " " + this.x + " " +…
- What is printed? java int count = 0; Runnable r = () -> System.out.print(count); count++; r.run();
- What is printed? java enum Level { LOW(1), HIGH(3); Level(int v) { System.out.print(name() + v + " "); } static { System.out.print("S ")…
- What is printed? java enum Dir { N, E, S, W } // in main: System.out.print(Dir.values()[2] + " " + Dir.valueOf("W").ordinal() + " ")…