Assignment 2-Java
Assignment 2-Java
a) Runtime error
b) Throws exception
d) Runs successfully
a) True
b) False
c) Referring to the instance variable when local variable has the same
name
a) Compilation error
b) Compilation succeeds
c) Runtime error
a) Compilation error
b) Runtime error
Question 2
class Base {
final public void show() {
System.out.println("Base::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
A Base::show() called
B Derived::show() called
C Compiler Error
D Runtime Error
Question 3
class Base {
public static void show() {
System.out.println("Base::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
A Base::show() called
B Derived::show() called
C Compiler Error
Question 4
Which of the following is true about inheritance in Java?
Question 5
Output of following Java program?
class Base {
public void Print() {
System.out.println("Base");
}
}
class Main{
public static void DoPrint( Base o ) {
o.Print();
}
public static void main(String[] args) {
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}
A Base
Derived
Derived
Base
B Base
Derived
Base
C Derived
Base
D Compiler Error
Question 6
Predict the output of following program. Note that fun() is public in base
and private in derived.
class Base {
public void foo() { System.out.println("Base"); }
}
Question 7
Which of the following is true about inheritance in Java.
1) In Java all classes inherit from the Object class directly or indirectly.
The Object class is root of all classes.
2) Multiple inheritance is not allowed in Java.
3) Unlike C++, there is nothing like type of inheritance in Java where we
can specify whether the inheritance is protected, public or private.
A
1, 2 and 3
B 1 and 2
C 2 and 3
D 1 and 3
Question 8
Predict the output of following Java Program
// filename Main.java
class Grandparent {
public void Print() {
System.out.println("Grandparent's Print()");
}
}
A
Compiler Error in super.super.Print()
B
Grandparent's Print()
Parent's Print()
Child's Print()
C
Runtime Error
Question 9
final class Complex {
class Main {
public static void main(String args[])
{
Complex c = new Complex(10, 15);
System.out.println("Complex number is " + c);
}
}
Complex number is
Complex@8e2fb5