Java MCQs
Java MCQs
class Automobile {
private String drive() {
return "Driving vehicle";
}
//first check in polymorpic reference class then main then rest
} class depend on accesibility modifier
class Car extends Automobile {
protected String drive() {
return "Driving car";
}
}
@Override
public final String drive() {
return "Driving an electric car";
}
A. Driving vehicle
B. Driving an electric car
C. Driving car
D. The code does not compile
2. Look at the following code and choose the right option for the word :
// Shape.java
public class Shape {
protected void display() {
System.out.println("Display-base");
}
}
// Circle.java
public class Circle extends Shape { <
< access - modifier > void display() {
System.out.println("Display-derived");
}
}
class Base {
public Base() {
System.out.println("Base");
}
}
a)
Base
Derived
DeriDerived
b)
Derived
DeriDerived
c)
DeriDerived
Derived
Base
d)
DeriDerived
Derived
class One{
public One(){
System.out.print("One,");
}
}
class Two extends One{
public Two(){
System.out.print("Two,");
}
}
class Three extends Two{
public Three(){
System.out.print("Three");
}
}
a) Three
b) One
c) One,Two,Three
d) Run-time error
class Base {
public Base() {
System.out.print("Base ");
}
public Base(String s) {
System.out.print("Base: " + s);
}
}
class Test {
public static void main(String[] args) {
Base base = new Derived("Hello ");
}
}
{
System.out.print("3");
}
}
a) 23451
b) 12354
c) 13245
d) The code does not compile.
class Math {
public final double secret = 2;
}
A. 2
B. 4
C. 8
D. The code does not compile.
class One{
public static void print(){
System.out.println("1");
}
}
a) 2
b) 1
c) Compile-time error
d) Run-time error
class Parent{
public void className(){
System.out.println("Parent");
}
}
class Child extends Parent{
void className(){
System.out.println("Child");
}
}
a) Parent
b) Child
c) Compile-time error
d) Run-time error
class Demo{
public Demo(int i){
System.out.println("int");
}
a) int
b) short
c) Compile-time error
d) Run-time error
Q1 - Consider the following program and predict the output:
b) long
c) Integer
d) int
s2 = s2.intern();
System.out.println(s1 == s2);
}
}
a) false
b) true
c) None
Q3 - What will be the output of the following program?
class Base {
public Base() {
System.out.println("Base");
}
}
a)
Base
Derived
DeriDerived
b)
Derived
DeriDerived
c)
DeriDerived
Derived
Base
d)
DeriDerived
Derived
Q4 - Consider the following program:
Which one of the following options correctly describes the output of this program?
a) foo(Integer)
b) foo(short)
c) foo(long)
d) foo(int ...)
Q5 - Look at the following code and choose the right option for the word :
// Shape.java
public class Shape {
protected void display() {
System.out.println("Display-base");
}
}
// Circle.java
public class Circle extends Shape { <
< access - modifier > void display() {
System.out.println("Display-derived");
}
}
void bar() {
System.out.println("In BaseClass.bar()");
}
void bar() {
System.out.println("In Derived.bar()");
}
}
Which one of the following options correctly describes the behavior of this program?
a)
This program results in a compiler error in the line marked with the comment
BASE_FOO_CALL.
In BaseClass.foo()
In BaseClass.bar()
In Derived.foo()
In Derived.bar()
class Base {
public void test() {
}
}
class Test {
public static void main(String[] args) {
Base obj = new Base1();
((Base2) obj).test(); // CAST
}
}
c) The compiler will report an error in the line marked with comment CAST.
Which one of the following options provides the output of this program when executed?
a)
s1 and s2 equal
s1 and s3 equal
b)
s1 and s2 equal
s1 and s3 not equal
c)
d)
a) 11511
b) 1105110
c) 115110
d) 27
Q1 - What will be the output of the following program?
class Base {
public Base() {
System.out.println("Base");
}
}
a)
Base
Derived
DeriDerived
b)
Derived
DeriDerived
c)
DeriDerived
Derived
Base
d)
DeriDerived
Derived
Q2 - Consider the following program:
class Base {
public Base() {
System.out.print("Base ");
}
public Base(String s) {
System.out.print("Base: " + s);
}
}
class Test {
public static void main(String[] args) {
Base base = new Derived("Hello ");
}
}
a) Removing Stmt-1 will make the program compilable and it will print the following: Base
Derived.
b) Removing Stmt-1 will make the program compilable and it will print the following: Base: Hello
Derived.
c) Removing Stmt-2 will make the program compilable and it will print the following: Base
Derived.
d) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the
following: Base Derived.
e) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the
following: Base: Hello Derived.
Q3 - Consider the following program and choose the correct option from the list of
options:
class Base {
public void test() {
}
}
class Test {
public static void main(String[] args) {
Base obj = new Base1();
((Base2) obj).test(); // CAST
}
}
c) The compiler will report an error in the line marked with comment CAST.
Q4 - Consider the following program and predict the behavior of this program:
class Base {
public void print() {
System.out.println("Base:print");
}
}
a) Compiler error “an abstract class cannot extend from a concrete class” at statement #1.
class SuperClass {
SuperClass() {
foo();
}
public SubClass() {
member = "HI";
}
a) In SuperClass.foo()
b) In Derived.foo(): hi
c) In SuperClass.foo() In Derived.foo(): hi
a) is-a
b) is-a-kind-of
c) has-a
d) is-implemented-in-terms-of
e) composed-as
void bar() {
System.out.println("In BaseClass.bar()");
}
void bar() {
System.out.println("In Derived.bar()");
}
}
Which one of the following options correctly describes the behavior of this program?
a)
This program results in a compiler error in the line marked with the comment
BASE_FOO_CALL.
In BaseClass.foo()
In BaseClass.bar()
In BaseClass.foo()
In Derived.bar()
In Derived.foo()
In Derived.bar()
Which one of the following options correctly describes the output of this program?
a) foo(Integer)
b) foo(short)
c) foo(long)
d) foo(int ...)
class Color {
int red, green, blue;
void Color() {
red = 10;
green = 10;
blue = 10;
}
void printColor() {
System.out.println("red: " + red + " green: " + green + " blue: " +
blue);
}
}
B. Compiles without errors, and when run, it prints the following: red: 0 green: 0 blue: 0.
C. Compiles without errors, and when run, it prints the following: red: 10 green: 10 blue: 10.
Q4 - Look at the following code and choose the right option for the word :
// Shape.java
public class Shape {
protected void display() {
System.out.println("Display-base");
}
}
// Circle.java
public class Circle extends Shape { <
< access - modifier > void display() {
System.out.println("Display-derived");
}
}
public class A {
public static void main(String[] args) {
String s1 = new String("javaguides");
String s2 = new String("javaguides");
System.out.println(s1 = s2);
}
}
s2 = s2.intern();
System.out.println(s1 == s2);
}
}
7. How many objects will be created in the following code and where they will be stored
in the memory?
String s1 = "javaguides";
String s2 = "javaguides";
8. How many objects will be created in the following code and where they will be stored?
String s2 = "javaguides";
String s1 = "javaguides"
String s2 = "javaguides";
System.out.println(s1 == s2);