323). Which of these keywords are used to define an abstract class?
[A]abst
[B]abstract
[C]Abstract
[D]abstract class
Show Answer
Correct Answer: abstract
Notes:
Answer: b
Explanation: None.
324). Which of these is not abstract?
[A]Thread
[B]AbstractList
[C]List
[D]None of the Mentioned
Show Answer
Correct Answer: Thread
Notes:
Answer: a
Explanation: Thread is not an abstract class.
325). If a class inheriting an abstract class does not define all of its function then it will be known as?
[A]Abstract
[B]A simple class
[C] Static class
[D]None of the mentioned
Show Answer
Correct Answer: Abstract
Notes:
Answer: a
Explanation: Any subclass of an abstract class must either implement all of the abstract method in the superclass or be itself declared abstract.
326). Which of these is not a correct statement?
[A] Every class containing abstract method must be declared abstract
[B]Abstract class defines only the structure of the class not its implementation
[C]Abstract class can be initiated by new operator
[D]Abstract class can be inherited
Show Answer
Correct Answer: Abstract class can be initiated by new operator
Notes:
Answer: c
Explanation: Abstract class cannot be directly initiated with new operator, Since abstract class does not contain any definition of implementation it is not possible to create an abstract object.
327). Which of these packages contains abstract keyword?
[A] java.lang
[B] java.util
[C]java.io
[D] java.system
Show Answer
Correct Answer: java.lang
Notes:
Answer: a
Explanation: None.
328). What will be the output of the following Java code?
class A
{
public int i;
private int j;
}
class B extends A
{
void display()
{
super.j = super.i + 1;
System.out.println(super.i + " " + super.j);
}
}
class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
[A]2 2
[B]3 3
[C]Runtime Error
[D]Compilation Error
Show Answer
Correct Answer: Compilation Error
Notes:
Answer: d
Explanation: Class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.
output:$ javac inheritance.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field A.j is not visible
329). What will be the output of the following Java code?
class A
{
public int i;
public int j;
A()
{
i = 1;
j = 2;
}
}
class B extends A
{
int a;
B()
{
super();
}
}
class super_use
{
public static void main(String args[])
{
B obj = new B();
System.out.println(obj.i + " " + obj.j)
}
}
[A]1 2
[B] 2 1
[C]Runtime Error
[D]Compilation Error
Show Answer
Correct Answer: 1 2
Notes:
Answer: a
Explanation: Keyword super is used to call constructor of class A by constructor of class B. Constructor of a initializes i & j to 1 & 2 respectively.
output:
$ javac super_use.java
$ java super_use
1 2
330). What will be the output of the following Java code?
class A
{
int i;
void display()
{
System.out.println(i);
}
}
class B extends A
{
int j;
void display()
{
System.out.println(j);
}
}
class method_overriding
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
[A]0
[B]1
[C]2
[D]Compilation Error
Show Answer
Correct Answer: 2
Notes:
Answer: c
Explanation: class A & class B both contain display() method, class B inherits class A, when display() method is called by object of class B, display() method of class B is executed rather than that of Class A.
output:
$ javac method_overriding.java
$ java method_overriding
2
331). What will be the output of the following Java code?
class A
{
public int i;
protected int j;
}
class B extends A
{
int j;
void display()
{
super.j = 3;
System.out.println(i + " " + j);
}
}
class Output
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
[A]1 2
[B] 2 1
[C]1 3
[D]3 1
Show Answer
Correct Answer: 1 2
Notes:
Answer: a
Explanation: Both class A & B have member with same name that is j, member of class B will be called by default if no specifier is used. I contains 1 & j contains 2, printing 1 2.
output:
$ javac Output.java
$ java Output
1 2