MCQ Practice Oop PDF
MCQ Practice Oop PDF
MCQ Practice Oop PDF
2. When a subclass constructor calls its superclass constructor, what happens if the superclass’s
constructor does not assign a value to an instance variable?
D. The program compiles and runs because the instance variables are initialized to their
default values.
3. When overriding a superclass method and calling the superclass version from the subclass
method, failure to prefix the superclass method name with the keyword super and a dot (.) in the
superclass method call causes ________.
A. a compile-time error.
B. a syntax error.
C. infinite recursion.
D. a runtime error.
A. A subclass object can assign an invalid value to the superclass’s instance variables, thus
leaving an object in an inconsistent state.
B. Subclass methods are more likely to be written so that they depend on the superclass’s
data implementation.
C. We may need to modify all the subclasses of the superclass if the superclass
implementation changes.
C. private constructors.
D. protected constructors.
class A
{
int a;
public A()
{
a = 7;
}
}
class B extends A
{
int b;
public B()
{
b = 8;
}
}
B. After the constructor for class B executes, the variable a will have the value 7.
C. After the constructor for class B executes, the variable b will have the value 8.
A. Integer.
B. Object.
C. String.
D. Class.
B. It's often much more efficient to create a class by inheriting from a similar class than to
create the class by writing every line of code the new class requires.
C. If the class you're inheriting from declares instance variables as private, the inherited
class can access those instance variables directly.
D. A class's instance variables are normally declared private to enforce good software
engineering.
C. The class following the extends keyword in a class declaration is the direct superclass of
the class being declared.
A. public access.
B. package access.
C. private access.
D. block scope.
13. Which statement best describes the relationship between superclass and subclass types?
14. If the superclass contains only abstract method declarations, the superclass is used for ________.
A. implementation inheritance.
B. interface inheritance.
C. Both.
D. Neither.
D. abstract superclasses must declare all data members not given values as abstract.
18. Consider classes A, B and C, where A is an abstract superclass, B is a concrete class that inherits
from A and C is a concrete class that inherits from B. Class A declares abstract method
originalMethod, implemented in class B. Which of the following statements is true of class C?
19. Which keyword is used to specify that a class will define the methods of an interface?
A. uses
B. implements
C. defines
D. extends
B. Class Collections contains many static helper methods for working with objects that
implement interfaces Collection, List, Set and more.
C. Collections method sort can sort objects of any class that implements interface List.
D. With non-static interface methods, helper methods can now be declared directly in
interfaces rather than in separate classes.
B. The throw statement is used to specify that a method will throw an exception.
B. The code in a finally block is executed only if an exception does not occur.
C. The code in a finally block is executed only if there are no catch blocks.
24. What is the difference between a try block and a try statement?
B. A try statement refers to the block of code following the keyword try, while the try block
refers to the try keyword and the block of code following this keyword.
C. The try block refers to the keyword try followed by a block of code. The try block and its
corresponding catch and/or finally clauses together form a try statement.
D. The try statement refers to the keyword try followed by a block of code. The try
statement and its corresponding catch and/or finally clauses together form a try block.
catch (ArithmeticException e)
{
System.out.println(e);
}
C. A finally block.
D. An exception handler.
26. All exception classes inherit, either directly or indirectly, from ________.
A. class Error.
B. class RuntimeException.
C. class Throwable.
A. empty constructor.
B. no-argument constructor.
C. default constructor.
D. null constructor.
A. Methods.
B. Constructors.
class A {
int i;
void display() {
System.out.println(i);
}
}
class B extends A {
int j;
void display() {
System.out.println(j);
}
}
class inheritance_demo {
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
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
class access{
public int x;
private int y;
void cal(int a, int b){
x = a + 1;
y = b;
}
}
class access_specifier {
public static void main(String args[])
{
access obj = new access();
obj.cal(2, 3);
System.out.println(obj.x + " " + obj.y);
}
}
A. 3 3
B. 2 3
C. Runtime Error
D. Compilation Error
abstract class XY
{
abstract sum(int x, int y){ }
}
D. No error.
I. String a;
A. I
B. II
C. III
D. IV
35. Which of the following is correct syntax for defining a new class Movie based on the superclass
Multimedia?
B. I and II
C. II and III
D. I
E. III
B. Only I and II
C. I, II and III
C. "X extends Y" is correct if X and Y are either both classes or both interfaces
D. "X extends Y" is correct for all combinations of X and Y being classes and/or
interfaces
A. knows-a relationship.
B. has-a relationship.
C. uses-a relationship.
D. is-a relationship.
40. Which of the following keywords allows a subclass to access a superclass method even when the
subclass has overridden the superclass method?
A. base.
B. super.
C. this.
D. public.
1 2 3 4 5 6 7 8 9 10
D D C D B D B B C
B
11 12 13 14 15 16 17 18 19 20
B B D B A D D B B
B
21 22 23 24 25 26 27 28 29 30
D A D C B C B C C
D
31 32 33 34 35 36 37 38 39 40
A C C A D C C D B
A
1. Assume Program1.java file is already compiled, which command is correct to run the program?
A. start Program1.java
B. run Program1
C. java Program1.java
D. java Program1
2. Which signatures is valid for the main() method to work as entry point of Java application?
4. What is the maximum number that can be stored in long type variable?
A. 264
B. 264 – 1
C. 263
D. 263-1
B. All objects of the class share one copy of instance variable, so it uses less memory
D. Instance variables are same as static, they just use less memory
public class A {
static int x;
public static void main(String[] args) {
A that1 = new A();
A that2 = new A();
that1.x = 5;
that2.x = 1000;
x = -1;
System.out.println(that2.x);
}
}
A. 0
B. 5
C. 1000
D. -1
A. Lines 5 and 12 will not compile because the method names and return types are missing.
B. Line 12 will not compile because you can only have one static initializer.
C. The code compiles and execution produces the output x = 10.
D. The code compiles and execution produces the output x = 15.
E. The code compiles and execution produces the output x = 3.
10. You want to create a table that looks like:
12 -9 8
7 14
-32 -1 0
A. double[][] table =
{ 12, -9, 8,
7, 14,
-32, -1, 0} ;
B. double[][] table =
{ {12, -9, 8},
{7, 14, 0},
-32, -1, 0} };
C. double[][] table =
{ {12, -9, 8}
{7, 14}
{-32, -1, 0} };
D. double[][] table =
{ {12, -9, 8},
{7, 14},
{-32, -1, 0} };
11. Which of following statement is true in context of static methods and static variables?
D. you can access instance variables in static methods using this keyword
A. Instance variables
B. Local variables
C. Final variables
D. Static variables
13. Why we use get and set methods to read and write instance variables?
B. I would encapsulate only if there is some rule (when the class was declared) that must be
verified before the changing the value of that instance attribute
C. Program consume less memory to store encapsulated instance attributes
D. I would encapsulate because we may need to impose some rule on data in future
A. public Test(void)
B. Test( )
C. Test(void)
D. public Test( )
E. None of these
16. Types in Java are divided into two categories ______ types and ________ types.
A. int
B. float
C. void
D. None of these
18. Which of following is a method having same name as that of its class?
A. finalize
B. constructor
C. delete
D. class
19. When multiple constructors are defined inside a class, how compiler differentiate overloaded
constructors?
A. By counting the number of constructors
20. Assume two constructors are defined with different signatures but they contain exactly same code
in their body. Which of following statement is correct?
A. Different constructors are used to initialize object in different ways, if both constructors
have same code, its compile time error
B. Code would compile successfully but at runtime, code would break or terminate
21. Assume you declared only one constructor that takes an argument. While creating the object of
that class, you passed no argument to constructor. Choose most appropriate option.
A. Its compile-time error because no-argument constructor is not defined
C. Code would not run even if you define no-argument constructor because only compiler
can provide no-argument constructor
D. This is an error, because all classes must contain more than one constructors
22. It’s possible to have several methods with the same name where method operate on different
types or numbers of arguments. This feature is called?
A. Encapsulation
B. Method Overloading
C. Constructor Overloading
D. Method Overriding
23. Which method is called implicitly when an object appears in code when a String is needed?
A. String()
B. toString()
C. printString()
D. print()
A. Has-A relation
B. Is-A relation
C. Belongs-To relation
D. Related-To relation
25. Which keyword is used to declare a variable whose value do not change once it is initialized at
the point of declaration?
A. static
B. final
C. private
D. fix
A. Objects
B. Object reference
A. The program has a compile error because the size of the array wasn't specified when
declaring the array.
B. The program has a runtime error because the array elements are not initialized.
D. The program has a runtime error because the array element x[0] is not defined
28. Analyze the following code and choose the correct answer.
A. The code has compile errors because the variable arr cannot be changed once it is
assigned.
B. The code would compile and run fine. The second line assigns a new array to arr.
C. The code has compile errors because we cannot assign a different size array to arr.
30. What is the value of a[1] after the following code is executed?
int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];
A. 0
B. 1
C. 2
D. 3
1 2 3 4 5 6 7 8 9 10 Marks
D B A D B C C D E D
11 12 13 14 15 16 17 18 19 20
C B C D D D D B B C
21 22 23 24 25 26 27 28 29 30
A B B A B A C B C B
Question
For the multiple choice questions given below, encircle the most suitable answer. Multiple selection and
cutting will lead to 0 marks.
1. An array object, ArrayOne, is created as:
float[][] ArrayOne;
ArrayOne = new float[20][10];
Suppose ArrayOne is passed as an argument to a method in which the corresponding parameter
is named someArray. What should the declaration of someArray look like in the parameter list
of the method?
a) float [][] someArray
b) float someArray[]
c) float [] someArray[20]
d) float someArray[20][10]
8. Line 11 is attempting to assign the value of h to the variable height. It was claimed that there
is an error in line 11. Which of the following is true?
a) The error is there’s no field h in the Desk class, so can’t write this.h. It should be
corrected to: this.height = h;
b) The error is the same as specified in option A, but it should be corrected to the
equation: h = height;
c) The error is simply that the right and left hand sides of the assignment are switched. It
should be corrected to: this.h = height;
d) There is no error.
9. Assume that any compilation (i.e. compile time) errors in the above code are corrected. If an
instance of this class is made with the instantiation:
Desk d = new Desk(1,2,3); What is the return value of calling
d.getLength()? The default value of integer field variables is 0.
a) 0
b) 1
c) 2
d) 3
10. You have two methods named calc in the same class that both return an integer, but one
takes 1 double and the other takes 2 doubles. We say method calc is:
a) Overridden
b) Overloaded
c) Instantiated
d) Invoked
12. What type of relationship exists between someMethod in classes A and someMethod in class B?
a) method overriding
b) method overloading
c) both method overriding and method overloading
d) neither method overriding nor method overloading
14. Which of the following is true about an abstract method inherited into a class C?
17. Providing access to an object only through its member functions, while keeping the details private
is called
a.b) Encapsulation
a.c) Inheritance
a.d) Modularity
18. In a student grading system, objects from different classes communicate with each other. These
communications are known as _____.
a.a) Inheritance
a.c) Polymorphism
19. Suppose the class Chair extends the class Furniture. Both classes are non-abstract. Which of the
following assignments are legal?
I. Chair c = new Chair();
II. Furniture f = new Furniture();
III. Furniture f = new Chair();
IV. Object o = new Chair();
a) I and II only
b) II and III only
21.When does Java know an object is no longer needed? And what happens to an unneeded
object's storage?
a) The programmer tells Java when an object is no longer needed by calling dispose() on
it; the object's memory is released back to the memory pool.
b) If there are no references to the object, Java knows the object is no longer needed and
automatically returns its memory to the memory pool.
c) If there are no references to the object, Java marks it as no longer needed; the
memory stays in use until the programmer explicitly returns it to the memory pool.
d) Objects, once constructed, stay active until the program terminates, so thought he
programmer may know an object is it no longer needed, Java does not know this;
objects' memory is returned to the memory pool when the program terminates.
e) Objects, once constructed, stay active until the method in which they were
constructed terminates, so though the programmer may know an object is no longer
needed, Java does not know this; objects' memory is returned to the memory pool
when the method terminates.
22.Which of the following Java statements set even to true if n is even, and to false if n is
odd? (n is an integer.) Assume n >= 0. (Even numbers are those integers which, when divided
by 2, have a remainder of 0.)
I. boolean even = (n/2.0 == (double)(n/2));
II. boolean even = (n % 2 == 0);
III. boolean even = (n div 2 == 0);
IV. boolean even = (n % 2 == n/2);
a) A > 5 || B != C
b) A >= 5 && B == C
c) !(A < 5) || (B != C)
d) A >= 5 || B == C
e) A < 5 && B == C