You are on page 1of 79

Revision

MCQs on Abstraction in Java


1) Below class ABC doesn’t have even a single
abstract method, but it has been declared as
abstract. Is it correct?
abstract class ABC
{
void firstMethod()
{
System.out.println("First Method");
}

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

public class MainClass


{
public static void main(String[] args)
{
int result = new Calculate()
{
@Override
int add(int a, int b)
{
return a+b;
}
}.add(11010, 022011);
}
}
Can you identify the error in the below code?

abstract class AbstractClass


{
private abstract int abstractMethod();
}
What will be the output of the following
program?
abstract class A
{
abstract void firstMethod();

void secondMethod()
{
System.out.println("SECOND");

firstMethod();
}
}

abstract class B extends A


{
@Override
void firstMethod()
{
System.out.println("FIRST");

thirdMethod();
}

abstract void thirdMethod();


}

class C extends B
{
@Override
void thirdMethod()
{
System.out.println("THIRD");
}
}

public class MainClass


{
public static void main(String[] args)
{
C c = new C();

c.firstMethod();

c.secondMethod();

c.thirdMethod();
}
}
What will be the output of the below program?
abstract class X
{
public X()
{
System.out.println("ONE");
}

abstract void abstractMethod();


}

class Y extends X
{
public Y()
{
System.out.println("TWO");
}

@Override
void abstractMethod()
{
System.out.println("THREE");
}
}

public class MainClass


{
public static void main(String[] args)
{
X x = new Y();

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) A subclass is a class that extends another class


b) A subclass is a class declared inside a class
c) Both above.
d) None of the above.
If class B is subclassed from class A then which is the
correct syntax in Java
a) class B:A{}
b) class B extends A{}
c) class B extends class A{}
d) class B implements A{}
Order of execution of constructors in Java Inheritance
is
a) Base to derived class
b) Derived to base class
c) Random order
d) none
Inheritance relationship in Java language is

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?

a) Yes—as long as single inheritance is followed.


b) No—inheritance is only between classes.
c) Only when one has been defined in terms of the other.
d) Yes—when one object is used in the constructor of another.
Which of these keywords is used to refer to member of
base class from a sub class?
a) this
b) super
c) upper
d) None of the above
Can a constructor be inherited?

a) A constructor can be inherited


b) A public constructor can be inherited
c) A protected constructor can be inherited
d) A constructor cannot be inherited
Given a parent class and a subclass, which of the
following is true?
a) Because of single inheritance, the subclass can have no subclasses.
b) Because of single inheritance, the subclass can have only one parent.
c) Because of single inheritance, the subclass can have only one child
classes.
d) Because of single inheritance, the subclass can have no siblings.
class Base {
public void show() {
System.out.println("Base show() called");
}
}
class Derived extends Base {
public void show() {
System.out.println("Derived show() called");
}
}
public class Main {
public static void main(String[] args) {
Base bb = new Derived(); // Base reference
bb.show();
}
}
class A{ public int i=10;}class B extends A{ protected int i=20;}class C
extends B{ private int i=30;}public class Main{ public static void
main(String[] args) { C b=new C(); System.out.println(b.i);
}}
class A{ static void StaticM() { System.out.println("Class A");
}}class B extends A{ static void StaticM() {
System.out.println("Class B"); }}public class Main{ public static
void main(String[] args) { B.StaticM(); }}
class A{
static String s ="AAA";
static {
s = s + "BBB";
}

{ 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) “is a kind of” relationship


(b) “has a” relationship
(c) “want to be” relationship
(d) inheritance does not describe any kind of relationship between classes
(e) “contains” of relationship.
1. Output of the following
class Circle{
Operation op;//aggregation
class Operation{ double pi=3.14;
double area(int radius){
int square(int n){ Operation op=new Operation();
return n*n; int rsquare=op.square(radius);
return pi*rsquare;
} }
} public static void main(String args[]){
Circle c=new Circle();
double result=c.area(5);
System.out.println(result);
}
}
3. How many ways are there to make a relationship
between classes?
a)1
b)2
c) 3
d)4
4. Object-oriented inheritance models the

(a) “is a kind of” relationship


(b) “has a” relationship
(c) “want to be” relationship
(d) inheritance does not describe any kind of relationship between classes
(e) “contains” of relationship.
5. Which one of these is a valid method declaration?
(a) void method1
(b) void method2()
(c) void method3(void)
(d) method4()
(e) methods(void).
6. Given a class named Book, which one of these
is a valid constructor declaration for the class?
(a) Book(Book b) { }
(b) Book Book() { }
(c) private final Book() { }
(d) void Book() { }
(e) abstract Book() { }.
7. Given the following definition of a class, which fields
are accessible from outside the package
com.corporation.project?
(a) Field i is accessible in all classes in other packages
package com.corporation.project;
public class MyClass (b) Field j is accessible in all classes in other packages
{
(c) Field k is accessible in all classes in other packages
int i;
public int j; (d) Field l is accessible in all classes in other packages
protected int k;
(e) Field l is accessible in subclasses only in other
private int l;
} packages.
8. every city exists in exactly one state, but a state can
have many cities, which is a _________ relationship.

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.

class B extends A{ C. An object of B contains data fields j, m.

private int k; D. An object of B contains data fields 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?

a) public members of class can be accessed by any code in the program


b) private members of class can only be accessed by other members of the class
c) private members of class can be inherited by a subclass, and become protected

members in subclass

d) protected members of a class can be inherited by a subclass, and become private


members of the subclass
12. Which among the following can’t restrict class
members to get inherited?

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

You might also like