Professional Documents
Culture Documents
void secondMethod()
{
System.out.println("Second Method");
}
}
2) Why the below class is showing compilation
error?
abstract class AbstractClass
{
abstract void abstractMethod()
{
System.out.println("First Method");
}
}
3) Which class is instantiable? Class A or Class
B?
abstract class A
{
class B extends A
{
}
4) Below code snippet is showing compilation
error? Can you suggest the corrections?
5) Is the following program written correctly? If
yes, what value “result” variable will hold
abstract class Calculate
{
abstract int add(int a, int b);
}
void secondMethod()
{
System.out.println("SECOND");
firstMethod();
}
}
thirdMethod();
}
class C extends B
{
@Override
void thirdMethod()
{
System.out.println("THIRD");
}
}
c.firstMethod();
c.secondMethod();
c.thirdMethod();
}
}
What will be the output of the below program?
abstract class X
{
public X()
{
System.out.println("ONE");
}
class Y extends X
{
public Y()
{
System.out.println("TWO");
}
@Override
void abstractMethod()
{
System.out.println("THREE");
}
}
x.abstractMethod();
}
}
Is the below code written correctly?
class X
{
abstract class Y
{
class Z
{
}
}
}
Is the below code written correctly?
class A
{
void methodOfA()
{
abstract class B
{
}
}
}
1. Given the following piece of code:
public class School{
public abstract double numberOfStudent();
}
which of the following statements is true?
A. The keywords public and abstract cannot be used together.
B. The method numberOfStudent() in class School must have a body.
C. You must add a return statement in method numberOfStudent().
D. Class School must be defined abstract.
2. Suppose A is an abstract class, B is a concrete subclass of A, and both A and
B have a default constructor. Which of the following is correct?
1. A a = new A();
2. A a = new B();
3. B b = new A();
4. B b = new B();
A. 1 and 2
B. 2 and 4
C. 3 and 4
D. 1 and 3
E. 2 and 3
3. Which of the following declares an abstract method in an abstract
Java class?
A. public abstract method();
B. public abstract void method();
C. public void abstract Method();
D. public void method() {}
E. public abstract void method() {}
4. Which of the following class definitions defines a legal abstract class?
A. class A { abstract void unfinished() { } }
B. class A { abstract void unfinished(); }
C. abstract class A { abstract void unfinished(); }
D. public class abstract A { abstract void unfinished(); }
5. Which of the following statements regarding abstract classes are
true?
A. An abstract class can be extended.
B. A subclass of a non-abstract superclass can be abstract.
C. A subclass can override a concrete method in a superclass to declare it
abstract.
D. An abstract class can be used as a data type.
E. All of the above
6. In Java, declaring a class abstract is useful
A. To prevent developers from further extending the class.
B. When it doesn't make sense to have objects of that class.
C. When default implementations of some methods are not desirable.
D. To force developers to extend the class not to use its capabilities.
E. When it makes sense to have objects of that class.
7. Which of these packages contains abstract keyword?
a) java.lang
b) java.util
c) java.io
d) java.system
MCQs on Inheritance
Interview | Exam Based
Which inheritance in java programming is not
supported
a) Multiple inheritance using classes
b) Multiple inheritance using interfaces
c) Multilevel inheritance
d) Single inheritance
What is subclass in java?
a) Association
b) Is-A
c) Has-A
d) None
Advantage of inheritance in java programming is/are
a) Code Re-usability
b) Class Extendibility
c) Save development time
d) All
Which cannot be inherited from a base class in Java
programming
a) Constructor
b) final method
c) Both
d) None
Which of this keyword must be used to inherit a class?
a) super
b) this
c) extent
d) extends
_______________ is one of the cornerstones of object-
oriented programming because it allows the creation
of hierarchical classifications.
A) Mutual Exclusion
B) Inheritance
C) Package
D) Interface
What restriction is there on using the super reference
in a constructor?
a) It can only be used in the parent's constructor.
b) Only one child class can use it.
c) It must be used in the last statement of the subclass' constructor.
d) It must be used in the first statement of the subclass' constructor.
Can an object subclass another object?
{ s = "ABABAB"; }
}
class B extends A{
static {
s = s + "BBBAAA"; }
{ System.out.println(s); }
}
public class Main{
public static void main(String[] args)
{
B b = new B();
}
}
ASSOCIATION MCQs
1. What is Association?
a) One – to – One
b) One – to – Many
c) Many – to – One
d) Many – to - Many
9. multiple students can be associated with a single
teacher and a single student can also be associated
with multiple teachers but both can be created or
deleted independently. This is a ______________
relationship.
a) One – to – One
b) One – to – Many
c) Many – to – One
d) Many – to - Many
ACCESS SPECIFIERS
1. Suppose a class has public visibility. In this class we define a protected method.
Which of the following statements is correct?
A. This method is only accessible from inside the class itself and from inside all
subclasses.
B. In a class, you cannot declare methods with a lower visibility than the visibility of
the class in which it is defined.
C. From within protected methods you do not have access to public methods.
D. This method is accessible from within the class itself and from within all classes
defined in the same package as the class itself.
2. Consider the following two classes declared and defined in two
different packages, what can be added in class B to form what
considered a correct access
1. Atto class
line1 A from
add noting; main()
At line2 method
add: new A(); of class
2. At line 1 add: import package.*; at line 2 add : new subPackage.A();
B? 3. At line 1 add: import subPackage.*; at line 2 add : new A();
package subPackage; 4. At line 1 add: import subPackage.A; at line 2 add : new A();
public class A { }
Options
A. 1 and 2
B. 2 and 4
package anotherPackage;
C. 3 and 4
// line 1 D. 1 and 3
public class B{
public static void main(String[] args){
// line 2
3. Which statements are most accurate regarding the following classes?
class A{
private int i;
protected int j;
A. An object of B contains data fields i, j, k, m.
}
B. An object of B contains data fields j, k, m.
protected int m;
}
4. A package is a collection of
A. Classes
B. Interfaces
C. Editing tools
D. Classes and Interfaces
E. Editing tools and Interfaces
5. What can directly access and change the value of the variable
qusNo?
package com.mypackage;
public class Test A. Only the Test class.
B. Any class.
{
C. Any class in com.mypackage
private int qusNo = 100;
package.
} D. Any class that extends Test.
E. None of these
6 . Which of these access specifiers must be used for main() method?
a) private
b) public
c) protected
d) none of the mentioned
7. Which of these is used to access a member of class before
object of that class is created?
a) public
b) private
c) static
d) protected
8. What will be the output of the following Java
code?
public class access_specifier
{
public static void main(String args[])
class access
{
{ access obj = new access();
public int x; obj.cal(2, 3);
System.out.println(obj.x + " " + obj.y);
private int y; }
}
void cal(int a, int b)
a) 3 3
{
x = a + 1;
b) 2 3
y = b; c) Runtime Error
} d) Compilation Error
}
9.What will be the output of the following Java code?
a) 7
7.4
class static_out public class static_use b) 6
{ {
6.4
static int x; public static void main(String args[])
{ c) 7
static int y;
void add(int a, int b) 9
static_out obj1 = new static_out();
static_out obj2 = new static_out();
{ int a = 2;
d) 9 7
x = a + b; obj1.add(a, a + 1);
y = x + b; obj2.add(5, a);
} System.out.println(obj1.x + " " + obj2.y);
} }
}
10. what will be the
output?
public class MainClass
{
package pack1; public static void main(String[] args)
class X {
{ X x = new X();
protected int i = 1221;
System.out.println(x.i);
void methodOfX()
{ x.methodOfX();
a) 1221
System.out.println(i); } 1221
} } b)Compilation error
c) No output
}
11. Which of the following statements are incorrect?
members in subclass
a) Private
b) Protected
c) Public
d) All three
13. You want subclasses in any package to have access to
members of a superclass. Which is the most restrictive access
that accomplishes this objective?
A. public
B. private
C. protected
D. transient
14. What is the most restrictive access modifier that
will allow members of one class to have access to
members of another class in the same package?
A. public
B. abstract
C. Protected
D. default access
15.Given a method in a protected class, what access modifier
do you use to restrict access to that method to only the other
members of the same class?
A. final
B. static
C. private
D. protected
E. volatile
Answers
1. D
2. C 11. C
3. B 12. A
13 .C
4. D
14 .D
5. A 15. C
6. B
7. C
8. C
9. C
10.A