0% found this document useful (0 votes)
8 views7 pages

Quiz

The document contains a series of Java programming questions and multiple-choice answers related to object-oriented concepts, method overriding, constructors, and type handling. Each question tests knowledge of Java inheritance, method visibility, and the behavior of constructors in class hierarchies. The answers provided are options labeled with letters corresponding to the correct output or behavior of the given Java code snippets.

Uploaded by

Mir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Quiz

The document contains a series of Java programming questions and multiple-choice answers related to object-oriented concepts, method overriding, constructors, and type handling. Each question tests knowledge of Java inheritance, method visibility, and the behavior of constructors in class hierarchies. The answers provided are options labeled with letters corresponding to the correct output or behavior of the given Java code snippets.

Uploaded by

Mir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1. What is the output of the following Java program?

class Automobile {
private String drive() {
return "Driving vehicle";
}
}

class Car extends Automobile {


protected String drive() {
return "Driving car";
}
}

public class ElectricCar extends Car {

@Override
public final String drive() {
return "Driving an electric car";
}

public static void main(String[] wheels) {


final Car car = new ElectricCar();
System.out.print(car.drive());
}
}

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");
}
}

a) Only the protected can be used.


b) public and protected both can be used.
c) public, protected, and private can be used.
d) Only the public can be used.

3. What will be the output of the following Java program?


class Base {
public Base() {
System.out.println("Base");
}
}

class Derived extends Base {


public Derived() {
System.out.println("Derived");
}
}

class DeriDerived extends Derived {


public DeriDerived() {
System.out.println("DeriDerived");
}
}

public class Test {


public static void main(String[] args) {
Derived b = new DeriDerived();
}
}

a)

Base
Derived
DeriDerived

b)

Derived
DeriDerived

c)

DeriDerived
Derived
Base

d)

DeriDerived
Derived

4. What is the output of the following Java program?

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");
}
}

public class Test{

public static void main(String[] args){


Three three = new Three();
}
}

a) Three
b) One
c) One,Two,Three
d) Run-time error

5. Consider the following program:

class Base {
public Base() {
System.out.print("Base ");
}

public Base(String s) {
System.out.print("Base: " + s);
}
}

class Derived extends Base {


public Derived(String s) {
super(); // Stmt-1
super(s); // Stmt-2
System.out.print("Derived ");
}
}

class Test {
public static void main(String[] args) {
Base base = new Derived("Hello ");
}
}

Select three correct options from the following list:


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.

6. What is the output of the following Java program?

abstract class Car {


static {
System.out.print("1");
}

public Car(String name) {


super();
System.out.print("2");
}

{
System.out.print("3");
}
}

public class BlueCar extends Car {


{
System.out.print("4");
}

public BlueCar() {
super("blue");
System.out.print("5");
}

public static void main(String[] gears) {


new BlueCar();
}
}

a) 23451
b) 12354
c) 13245
d) The code does not compile.

7. What is the output of the following Java program?

class Math {
public final double secret = 2;
}

class ComplexMath extends Math {


public final double secret = 4;
}
public class InfiniteMath extends ComplexMath {
public final double secret = 8;

public static void main(String[] numbers) {


Math math = new InfiniteMath();
System.out.print(math.secret);
}
}

A. 2
B. 4
C. 8
D. The code does not compile.

8. What is the output of the following Java program?

public class Test {


public void print(Integer i) {
System.out.println("Integer");
}

public void print(int i) {


System.out.println("int");
}

public void print(long i) {


System.out.println("long");
}

public static void main(String args[]) {


Test test = new Test();
test.print(10);
}
}

a) The program results in a compiler error (“ambiguous overload”).


b) long
c) Integer
d) int

9. What is the output of the following Java program?

class One{
public static void print(){
System.out.println("1");
}
}

class Two extends One{


public static void print(){
System.out.println("2");
}
}

public class Test{


public static void main(String args[]){
One one = new Two();
one.print();
}
}

a) 2
b) 1
c) Compile-time error
d) Run-time error

10. What is the output of the following Java program?

class Parent{
public void className(){
System.out.println("Parent");
}
}
class Child extends Parent{
void className(){
System.out.println("Child");
}
}

public class Test{

public static void main(String[] args){


Parent parent = new Child();
parent.className();
}
}

a) Parent
b) Child
c) Compile-time error
d) Run-time error

11. What is the output of the following Java program?

class Demo{
public Demo(int i){
System.out.println("int");
}

public void Demo(short s){


System.out.println("short");
}
}

public class Test{

public static void main(String[] args){


short s = 10;
Demo demo = new Demo(s);
}
}

a) int
b) short
c) Compile-time error
d) Run-time error

You might also like