MCQ
MCQ
MCQ
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.
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
A. Prior to Java SE 8, it was common to associate with an interface a class containing static
helper methods for working with objects that implemented the interface.
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;
II. String b = new String();
III. String c = "";
IV. String d = "null";
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. 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?
A. public static void main()
B. public static void main(String arg[])
C. public static int main(String [] arg)
D. public static String[] main(String[] arg)
4. What is the maximum number that can be stored in long type variable?
A. 264
B. 264 – 1
C. 263
D. 263-1
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.
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} };
13.Why we use get and set methods to read and write instance variables?
A. We can’t access instance variables without get/set methods
B. JVM performance is effected when we access instance variables
directly
C. Get/set methods allows to impose constraints before reading or writing
D. Get/set methods increase the program speed when called correctly
16.Types in Java are divided into two categories ______ types and ________ types.
A. Object and Variable
B. Class and Object
C. Primitives and Classes
D. Primitive and Reference
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
C. There is no compile-time or runtime issue even if both constructors’
body is same
D. Code would compile but at runtime, unexpected result would be
generated
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
B. Code is correct as compiler provide default constructor that is a no-
argument constructor
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
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. 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.
C. The program runs fine and displays x[0] is 0
D. The program has a runtime error because the array element x[0] is not
defined
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.
D. It compile successfully if we also change the size of second array from
6 to 5
A. 0
B. 1
C. 2
D. 3
Section 1- Multiple choice questions.
2. Which keyword is used by method to refer to the object that invoked it?
a) import
b) catch
c) abstract
d) this
3. Which operator is used by Java run time implementations to free the memory of an object when it
is no longer needed?
a) delete
b) free
c) new
d) None of the mentioned
1. class equality {
2. int x;
3. int y;
4. boolean isequal() {
5. return(x == y);
6. }
7. }
8. class Output {
9. public static void main(String args[])
10. {
11. equality obj = new equality();
12. obj.x = 5;
13. obj.y = 5;
14. System.out.println(obj.isequal);
15. }
16. }
a) false
b) true
c) 0
d) 1
5. Which of the following statements are incorrect?
a) Default constructor is called at the time of declaration of the object if a constructor has not
been defined.
b) Constructor can be parameterized.
c) finalize() method is called when a object goes out of scope and is no longer needed.
d) finalize() method must be declared protected.
1. class area {
2. int width;
3. int length;
4. int area;
5. void area(int width, int length) {
6. this.width = width;
7. this.length = length;
8. }
9.
10. }
11. class Output {
12. public static void main(String args[])
13. {
14. area obj = new area();
15. obj.area(5 , 6);
16. System.out.println(obj.length + " " + obj.width);
17. }
18. }
a) 0 0
b) 5 6
c) 6 5
d) 5 5
7. Which of this access specifiers can be used for a class so that its members can be accessed by a
different class in the same package?
a) Public
b) Protected
c) No Modifier
d) All of the mentioned
8. Which of these access specifiers can be used for a class so that it’s members can be accessed by a
different class in the different package?
a) Public
b) Protected
c) Private
d) No Modifier
a) 0
b) value stored in arr[0].
c) 00000
d) Some reference value
a) 3
b) 0
c) 6
d) 1
a) 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 3 4 5 6 7 8 9 10
c) i j k l m n o p q r
d) i i i i i i i i i i
19. What is the process of defining more than one method in a class differentiated by method
signature?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned
20. What is the stored in the variable obj of type Box in following lines of code?
Box obj;
a) Memory address of allocated memory of object.
b) NULL
c) Any arbitrary pointer
d) Garbage
String y = "abc";
x = x + y;
a) 2
b) 3
c) 1
b) An indirect superclass is any class above the direct superclass in the class hierarchy
d) Signle inheritance means each class is derived from at least one direct super class
b) Car-steering wheel
c) Car-truck
d) Car-showroom
33. All public and protected superclass members retain their original access modifier when they
become members of the subclass. What about superclass’s private members:
a) They are not directly accessible outside the class itself.
b) They’re hidden in its subclasses and can be accessed only through the public or protected
methods inherited from the superclass.
c) They can be accessed from the subclass by preceding the superclass private member name
with keyword super and a dot (.) separator
1. class SuperClass{
2. int num;
3. SuperClass(){
4. num=0;
5. System.out.println("Super class's no argument constructor");
6. }
7. SuperClass(int n){
8. num=n;
9. System.out.println("Super class's parameterized constructor");
10. }
11. }
34. What happens if we create an instance of SubClass in main() method as SubClass objSub=new
SubClass();
a) There will be an output “Super class's no argument constructor”
b) Default constructor is not private in superclass but it can not be called explicitly from
constructor of its base class
c) Constructors are not inherited, so class SubClass does not inherit class SuperClass’s
constructor. However, SuperClass’s constructors are still available to SubClasses so there will
be no error
d) This is not a valid statement to call constructor of a superclass explicity, it should be called
with just writing super keyword and parethesis: super()
b) Java implicitly calls the SubClass’s default constructor and then SuperClass’s default or no-
argument constructor.
c) Java will implicitly call the default constructor of SubClass and the first task of that subclass
constructor will be to call SuperClass’s constructor
b) Is necessary to override a superclass method in base class, compiler will give an error
message otherwise.
d) It is your choice; if you use the annotation, the method is overridden, if you don’t , method
with same signature is made in the subclass class but is not overridden.
38. What will happen when we call myMthod() through an object of SubClass in the following code
1. class SuperClass{
2. void myMethod(){
3. System.out.print("superclass version of myMethod");
4. }
5. }///
6. class SubClass extends SuperClass{
7. void myMethod(){
8. myMethod();
9. System.out.println(" subclass version of
myMethod");
10. }
11. }
a) Compilation Error because @Override is not used while overriding myMehtod() in SubClass
b) Compilation Error because two methods with same signatures can not exist in a Superclass
and its subclass
c) It will cause the SubClass method myMwthod() to call itself, potentially creating an error
called infinite recursion
40. Say that class Mammal has a child class Whale and another child class Ape. Class Ape has a child
class Monkey. Examine the following
Mammal mml;
a) mml = whl;
b) mml=ape;
c) mnk=null;
d) mnk=whl;
41. What will happen if we write the following line of code in the above question scenario
42. For which of the following would polymorphism not provide a clean solution?
a) A billing program where there is a variety of client types that are billed with different fee
structures.
b) A maintenance log program where data for a variety of types of machines is collected and
maintenance schedules are produced for each machine based on the data collected.
c) A program to compute a 5% savings account interest for a variety of clients.
d) An IRS program that maintains information on a variety of taxpayers and determines who to
audit based on criteria for classes of taxpayers.
43. Multiple inheritance is not supported in case of class. But it is supported in case of interface
because:
a) Class may or may not be abstract while interface gives 100% abstraction.
d) Methods of an interface cannot be overridden in the class that implements that interface.
45. Consider a super class namely SuperClass and a sub class namely SubClass , what does the
following lines of code mean:
SuperClass obS=new SuperClass();
SubClass obSub=new SubClass();
obSub=(SubClass)obS;
a) It is used to test whether the object is an instance of the specified type (class or subclass or
interface).
a) It cannot be extended.
b) The abstract class can be used to provide some implementation of the interface.
d) All of above
ii. If you make any class as final, you cannot extend it.
iv. If you make any class as final, you cannot instantiate it.
a) Only i and ii
d) None is true
a) The compiler will issue an error message indicating that the exception must be caught.
b) The compiler will issue an error message indicating that the exception must be caught or
declared.
c) A stack trace will be displayed indicating the exception that has occurred and where it
occurred.
d) A stack trace will be displayed, along with a message indicating that the exception must be
caught.
b) The classes that extend Throwable class are known as checked exceptions
c) Tthese are not checked at compile-time rather they are checked at runtime.
b) DivideByZeroException
c) InfinitException
54. If we have null value in any variable, performing any operation by the variable occurs a
NullPointerException. Which of following options explains it while keeping in view this line of
code: String s=null;
a) System.out.println(s);
b) System.out.println(s.length( ));
c) String s1=s;
d) String s2=” ” + s;
55. What is the scenario where ArrayIndexOutOfBoundsException occurs while considering the
following line of code:
int a[]=new int[5];
a) a[4]=5;
b) int b=a[5];
c) a[4]= a[4]+1;
d) all of above
b) catch
c) finalize
d) throws
b) An exception is first thrown from the bottom of the stack and if it is not caught, it goes up
the call stack to the next method, If not caught there, the exception again goes up to the
next method, and so on until they are caught or until they reach the very top of the call
stack.
c) Stack is not concerned with exception propagation
d) Method activation stack converts to heap for Exception handling
60. ---- package contains all the classes required for input and output operations.
a) Java.iostream
b) Java.io
c) Java.file
d) Java.filestream
61. Java application uses a/an ----to write data to a destination, it may be a file, an array, peripheral
device or socket.
a) output stream
b) input stream
c) err stream
d) all of above
a) In java, 3 streams are created for us automatically. All these streams are attached with file.
c) Option b is right but all these streams are attached with console.
d) None of them
1. import java.io.FileOutputStream;
8. fout.write(b);
9. fout.close();
10. System.out.println("success...");
12. }
13. }
a) The content of a java file FileOutputStreamExample.java is set with the data from File
testout.txt
b) The content of a text file testout.txt is set with the data File handling is an interesting topic.
c) The content of a text file testout.txt is set with whatever is written by user on input console
d) All three possibilities are there depending upon the platform on which JVM runs
e) The code will not compile successfully as it has a compiler error at line 5.
64. What is the right code to create a BufferedReader object for a file D:/Sample.txt
a) reader = newBufferedReader(newReader("C:\\sample.txt"));
b) reader = newBufferedReader(newFileReader("C:\\sample.txt"));
a) When the user interacts with a GUI source code, the interaction—known as an event—drives
the program to perform a task.
b) When the user interacts with a GUI component, the interaction—known as an event—drives
the program to perform a task.
c) When the user interacts with a GUI component, the interaction—known as an eventHandler
—drives the program to perform a task.
d) When the user interacts with a GUI JButton, the interaction—known as an event—drives the
program to perform a task.
66. The code that performs a task in response to an event is called a/an ----
a) Event handler
b) Event driven
c) Event Handling
d) Event
67. event handlers are often implemented as private inner classes because
b) no class can be private because a private class cannot be accessed even if it is defined as inner
class of the class trying to access.
a) isSelected
b) isChecked
c) Selected
d) Checked
a) An inner class that is declared without a name and typically appears inside a method
declaration.
b) As with other inner classes, an anonymous inner class can access its top-level class’s
members.
c) An anonymous inner class has limited access to the local variables of the method in which it’s
declared.
d) Since an anonymous inner class has no name, one object of the anonymous inner class must
be created at the point where the class is declared.
71. Which of following is a valid statement to create a button with string label Click on it
72. How the JButton object b created previously can be added to a JFrame object Jf
a) b=add(Jf);
b) Jf=add(b);
c) Jf.add(b);
d) b.add(Jf);
75. --- A drop-down list of items from which the user can make a selection
a) JButton
b) JCheckBox
c) JComboBox
d) JList
d. a & b
e. All a, b & c
d. All of above
c) The compiler differentiates signatures by the number of parameters, the types of the
parameters and the order of the parameter types in each signature.
To overload constructors, simply provide multiple constructor declarations with different signatures and
return type.
Question.1 [1*10 = 10 Marks]
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
class A
{
public void someMethod(String x)
{
System.out.println( "from class A " + x );
}
}
-----------------------
class B extends A
{
public void someMethod( String x )
{
System.out.println( "from class B:" + x );
}
}
a) method overriding
b) method overloading
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 sub class, and become protected members in
sub class.
d) protected members of a class can be inherited by a sub class, and become private members of
the sub class.
14. Which of the following is true about an abstract method inherited into a class C?
a)The operating system periodically deletes all of the java files available on the
system.
c)When all references to an object are gone, the memory used by the object is
automatically reclaimed.
d) The JVM checks the output of any Java program and deletes anything
that doesn't make sense.
17. Providing access to an object only through its member functions, while keeping the details private
is called
a)Information hiding
b) Encapsulation
c)Inheritance
d) Modularity
18. In a student grading system, objects from different classes communicate with each other. These
communications are known as _____.
a)Inheritance
b) Message Passing
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
Solution
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 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
1 2 3 4 5 6 7 8 9 10
D D D B C C D A D D
11 12 13 14 15 16 17 18 19 20
A A D D C A B D B B
21 22 23 24 25 26 27 28 29 30
a A c a a a a b b d
31 32 33 34 35 36 37 38 39 40
d A D d d c a c d d
41 42 43 44 45 46 47 48 49 50
b C B a d d b a b b
51 52 53 54 55 56 57 58 59 60
a D A b b c a a d b
61 62 63 64 65 66 67 68 69 70
a C B b b a a c a e
71 72 73 74 75 76 77 78 79 80
b C D e c c a d b c