Sample questions
Question 1Medium
What will be the output of the program?
class Super
{
public Integer getLength()
{
return new Integer(4);
}
}
public class Sub extends Super
{
public Long getLength()
{
return new Long(5);
}
public static void main(String[] args)
{
Super sooper = new Super();
Sub sub = new Sub();
System.out.println(
sooper.getLength().toString() + "," + sub.getLength().toString() );
}
}
- 4, 4
- 4, 5
- 5, 4
- Compilation fails.
Answer: D. Compilation fails.
Explanation
D is correct, compilation fails - The return type of getLength( ) in the super class is an object of reference type Integer and the return type in the sub class is an object of reference type Long. In other words, it is not an override because of the change in the return type and it is also not an overload because the argument list has not changed
Step-by-step solution
- Super.getLength() returns an Integer, and Sub.getLength() returns a Long.
- An overriding method must return the same type as the parent method or a subtype of it.
- Long is not a subtype of Integer, and the parameter lists are the same, so Sub's method is not a valid override or overload.
- The compiler rejects the code, so the correct answer is option D.
Why the other options are wrong
- A. The program does not compile, so it prints nothing.
- B. The program does not compile, so it prints nothing.
- C. The program does not compile, so it prints nothing.
Tip: An overriding method's return type must be the same as, or a subtype of, the parent's return type.
Question 2Medium
What is the output of this program?
class box {
int width;
int height;
int length;
}
class mainclass {
public static void main(String args[])
{
box obj1 = new box();
box obj2 = new box();
obj1.height = 1;
obj1.length = 2;
obj1.width = 1;
obj2 = obj1;
System.out.println(obj2.height);
}
}
- 1
- 2
- Runtime error
- Garbage value
Answer: A. 1
Explanation
obj2 = obj1 copies the reference, height is 1
Step-by-step solution
- obj1 and obj2 are two separate box objects.
- obj2 = obj1 makes obj2 point to the same object as obj1.
- obj1.height was set to 1, so obj2.height is also 1.
- The program prints 1, so the answer is option A.
Why the other options are wrong
- B. 2 is the length of obj1, not the height.
- C. The code is valid, so no runtime error happens.
- D. The height was set to 1, so there is no garbage value.
Tip: Assigning one object reference to another copies the reference, not the object.
Question 3Medium
What will be the output of the program?
public class Test
{
public int aMethod()
{
static int i = 0;
i++;
return i;
}
public static void main(String args[])
{
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
- 0
- 1
- 2
- Compilation fails.
Answer: D. Compilation fails.
Explanation
Compilation failed because static was an illegal start of expression - method variables do not have a modifier (they are always considered local).
Step-by-step solution
Static int i = 0; is invalid inside aMethod() as static cannot be used for local variables.- The code will fail to compile due to this error.
- Compilation fails.
Why the other options are wrong
- A. Nothing prints 0, because the code fails to compile before it runs.
- B. No value is printed because the static local variable breaks the compile.
- C. 2 would need a kept counter, but static is illegal on a local, so the code fails.
Tip: Static cannot be used for local variables in methods.
Question 4Medium
Which of the following code fragments inserted, will allow to compile?
interface Base
{
boolean m1 ();
byte m2(short s);
}
which two code fragments will compile?
interface Base2 implements Base {}
abstract class Class2 extends Base
{ public boolean m1(){ return true; }}
abstract class Class2 implements Base {}
abstract class Class2 implements Base
{ public boolean m1(){ return (7 > 4); }}
abstract class Class2 implements Base
{ protected boolean m1(){ return (5 > 7) }}
- 1 and 2
- 2 and 3
- 3 and 4
- 1 and 5
Answer: C. 3 and 4
Explanation
because interface methods are implicitly public, so the methods being implemented must be public.
- (3) is correct because an abstract class doesn't have to implement any or all of its interface's methods.
- (4) is correct because the method is correctly implemented ((7 > 4) is a boolean).
- (1) is incorrect because interfaces don't implement anything.
- (2) is incorrect because classes don't extend interfaces.
- (5) is incorrect
Step-by-step solution
- Fragment 1 is wrong: an interface uses extends for other interfaces, not implements.
- Fragment 2 is wrong: a class uses implements for an interface, not extends.
- Fragments 3 and 4 compile: an abstract class may leave interface methods unimplemented, and m1 is public and returns a boolean.
- Fragment 5 fails because protected reduces the access of m1 and a semicolon is missing. So fragments 3 and 4 compile: option C.
Why the other options are wrong
- A. Fragment 1 is an interface that implements another interface, which is not allowed, so 1 and 2 both fail.
- B. Fragment 2 is a class that extends an interface, which is not allowed.
- D. Fragment 5 fails because protected is weaker than the public interface method; fragment 1 also fails.
Tip: A class implements interfaces, an interface extends interfaces, and interface methods must be public when implemented.
Question 5Medium
What is the output of this program?
class A {
int i;
int j;
A() {
i = 1;
j = 2;
}
}
class Output {
public static void main(String args[])
{
A obj1 = new A();
A obj2= new A();
System.out.print(obj1.equals(obj));
}
}
- false
- true
- 1
- Compilation Error
Answer: D. Compilation Error
Explanation
obj is not declared, so the code does not compile
Step-by-step solution
- Class Output calls obj1.equals(obj).
- obj is never declared; the program only declares obj1 and obj2.
- Java checks names before running, so the program does not compile.
- The output is a compilation error, option D.
Why the other options are wrong
- A. The program never runs because of the compile error.
- B. The program never runs, so true is not printed.
- C. equals() returns a boolean, not 1, and the code does not run anyway.
Tip: Check every variable name is declared before the line that uses it.
Question 6Medium
What will be the output of the program?
class Super
{
public int i = 0;
public Super(String text) /* Line 4 */
{
i = 1;
}
}
class Sub extends Super
{
public Sub(String text)
{
i = 2;
}
public static void main(String args[])
{
Sub sub = new Sub("Hello");
System.out.println(sub.i);
}
}
- 0
- 1
- 2
- Compilation fails.
Answer: D. Compilation fails.
Explanation
A default no-args constructor is not created because there is a constructor supplied that has an argument, line 4. Therefore the sub-class constructor must explicitly make a call to the super class constructor:
public Sub(String text)
{
super(text); // this must be the first line constructor
i = 2;
}
Step-by-step solution
- Super declares only the constructor Super(String text), so it has no no-argument constructor.
- Sub(String text) does not call super(...) itself, so Java inserts super() automatically.
- That call to super() has no matching constructor in Super, so the code does not compile.
- Nothing runs, so the correct answer is option D, compilation fails.
Why the other options are wrong
- A. The value 0 is never printed because the program does not compile.
- B. The value 1 would come from Super's constructor, which is never run.
- C. The value 2 would be set in Sub, but the code does not compile, so nothing prints.
Tip: If a superclass has only constructors with parameters, each subclass constructor must call super(args) on its first line.
Question 7Medium
Which is valid in a class that extends class A?
class A
{
protected int method1(int a, int b)
{
return 0;
}
}
public int method1(int a, int b) {return 0; }private int method1(int a, int b) { return 0; }public short method1(int a, int b) { return 0; }static protected int method1(int a, int b) { return 0; }
Answer: A. public int method1(int a, int b) {return 0; }
Explanation
A is correct - because the class that extends A is just simply overriding method1.
B is wrong - because it can't override as there are less access privileges in the subclass method1.
C is wrong - because to override it, the return type needs to be an integer. The different return type means that the method is not overriding but the same argument list means that the method is not overloading. Conflict - compile time error.
D is wrong - because you can't override a method and make it a class method i.e. using static.
Step-by-step solution
- Method in subclass must have same name, parameters, and return type as in superclass.
- Option A has same name, parameters, and return type as method1 in class A.
- Option B is private, which is less accessible than protected in superclass, so cannot override.
- Option C changes return type to short, which is not compatible with int, so no override.
- Option D uses static, but overriding requires instance methods, not static methods.
- Final answer: public int method1(int a, int b) {return 0; }
Why the other options are wrong
- B. private is less accessible than protected, so it cannot override method1 and would fail to compile as an override.
- C. Changing the return type to short is not compatible with int, so this does not override method1.
- D. static methods cannot override an instance method, so this static protected method does not override method1.
Tip: Override means same name, parameters, and return type. Static methods cannot be overridden.
Question 8Medium
What is the prototype of the default constructor?
public class Test { }
- Test( )
- Test(void)
- public Test( )
- public Test(void)
Answer: C. public Test( )
Explanation
A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class).
D is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out.
Step-by-step solution
- Default constructor has same access modifier as class, which is
public. - Constructor name must match class name,
Test. - No parameters, so no
void or any other type in parentheses. - Correct syntax is
public Test( ).
Why the other options are wrong
- A. A default constructor has the class access modifier, which is public here, so public must be shown.
- B. void is not a parameter in Java, so Test(void) is not a valid constructor prototype.
- D. The void in brackets is invalid Java syntax; a constructor with no parameters uses empty brackets.
Tip: Remember: default constructor has same access as class and no parameters.
Question 9Medium
You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?
- public
- private
- protected
- transient
Answer: C. protected
Explanation
Access modifiers dictate which classes, not which instances, may access features.
Methods and variables are collectively known as members. Method and variable members are given access control in exactly the same way.
- private makes a member accessible only from within its own class
- protected makes a member accessible only to classes in the same package or subclass of the class
- default access is very similar to protected (make sure you spot the difference) default access makes a member accessible only to classes in the same package.
- public means that all other classes regardless of the package that they belong to, can access the member (assuming the class itself is visible)
- final makes it impossible to extend a class, when applied to a method it prevents a method from being overridden in a subclass, when applied to a variable it makes it impossible to reinitialise a variable once it has been initialised
- abstract declares a method that has not been implemented.
- transient indicates that a variable is not part of the persistent state of an object.
- volatile indicates that a thread must reconcile its working copy of the field with the master copy every time it accesses the variable.
After examining the above it should be obvious that the access modifier that provides the most restrictions for methods to be accessed from the subclasses of the class from another package is C - protected. A is also a contender but C is more restrictive, B would be the answer if the constraint was the "same package" instead of "any package" in other words the subclasses clause in the question eliminates default.
Step-by-step solution
- Protected allows access to subclasses in any package.
- Public allows access to all classes regardless of package.
- Private restricts access to only the same class.
- Default access is similar to protected but limited to same package.
- Protected is most restrictive for subclass access across packages.
- Final answer: protected.
Why the other options are wrong
- A. public gives access to all classes everywhere, which is less restrictive than protected.
- B. private limits access to the same class, so subclasses cannot use the member.
- D. transient is for serialization and does not control subclass access.
Tip: Use protected for superclass members accessible to subclasses in any package.
Question 10Medium
What will be the output of the program?
class Base
{
Base()
{
System.out.print("Base");
}
}
public class Alpha extends Base
{
public static void main(String[] args)
{
new Alpha(); /* Line 12 */
new Base(); /* Line 13 */
}
}
- Base
- BaseBase
- Compilation fails
- The code runs with no output
Answer: B. BaseBase
Explanation
B is correct. It would be correct if the code had compiled, and the subclass Alpha had been saved in its own file. In this case Java supplies an implicit call from the sub-class constructor to the no-args constructor of the super-class therefore line 12 causes Base to be output. Line 13 also causes Base to be output.
A is wrong. It would be correct if either the main class or the subclass had not been instantiated.
C is wrong. The code compiles.
D is wrong. There is output.
Step-by-step solution
New Alpha() calls Base constructor, prints 'Base'.New Base() also calls its own constructor, prints 'Base'.- So total output is 'BaseBase'.
- Final answer: BaseBase.
Why the other options are wrong
- A. Only one Base is printed, but new Base() on line 13 also runs its constructor, so there are two.
- C. The code compiles; Base has a constructor that is accessible in the same file.
- D. Constructors print Base each time they run, so the program does produce output.
Tip: Remember: instantiating a subclass also runs the superclass constructor.
Practise all 49 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →