Java MCQ Set 1
1. What is not type of inheritance?
a) Single inheritance
b) Double inheritance
c) Hierarchical inheritance
d) Multiple inheritance
Answer
Answer: b [Reason:] Inheritance is way of acquiring attributes and methods of parent class. Java supports hierarchical inheritance directly.
2. Using which of the following, multiple inheritance in Java can be implemented?
a) Interfaces
b) Multithreading
c) Protected methods
d) Private methods
Answer
Answer: a [Reason:] Multiple inheritance in java is implemented using interfaces. Multiple interfaces can be implemented by a class.
3. All classes in Java are inherited from which class?
a) java.lang.class
b) java.class.inherited
c) java.class.object
d) java.lang.Object
Answer
Answer: d [Reason:] All classes in java are inherited from Object class. Interfaces are not inherited from Object Class.
4. In order to restrict a variable of a class from inheriting to sub class, how variable should be declared?
a) Protected
b) Private
c) Public
d) Static
Answer
Answer: b [Reason:] By declaring variable private, the variable will not be available in inherited to sub class.
5. If super class and sub class have same variable name, which keyword should be used to use super class?
a) super
b) this
c) upper
d) classname
Answer
Answer: a [Reason:] Super keyword is used to access hidden super class variable in sub class.
6. Static members are not inherited to sub class.
a) True
b) False
Answer
Answer: b [Reason:] Static members are also inherited to sub classes.
7. Which of the following is used for implementing inheritance through interface?
a) inherited
b) using
c) extends
d) implements
Answer
Answer: d [Reason:] Interface is implemented using implements keyword. A concrete class must implement all the methods of an iterface, else it must be declared abstract.
8. Which of the following is used for implementing inheritance through class?
a) inherited
b) using
c) extends
d) implements
Answer
Answer: c [Reason:] Class can be extended using extends keyword. One class can extend only one class. A final class cannot be extended.
9. What would be the result if class extends two interfaces and both have method with same name and signature?
a) Runtime error
b) Compile time error
c) Code runs successfully
d) First called method is executed successfully
Answer
Answer: b [Reason:] In case of such conflict, compiler will not be able to link a method call due to ambiguity. It will throw compile time error.
10. Does Java support multiple level inheritance?
a) True
b) False
Answer
Answer: a [Reason:] Java supports multiple level inheritance through implementing multiple interfaces.
Java MCQ Set 2
1. Which of the below is not a subinterface of Queue?
a) BlockingQueue
b) BlockingEnque
c) TransferQueue
d) BlockingQueue
Answer
Answer: b [Reason:] BlockingQueue, TransferQueue and BlockingQueue are subinterfaces of Queue.
2. What is the remaining capacity of BlockingQueue whose intrinsic capacity is not defined?
a) Integer.MAX_VALUE
b) BigDecimal.MAX_VALUE
c) 99999999
d) Integer.INFINITY
Answer
Answer: a [Reason:] A BlockingQueue without any intrinsic capacity constraints always reports a remaining capacity of Integer.MAX_VALUE.
3. PriorityQueue is threadsafe?
a) True
b) False
Answer
Answer: a [Reason:] PriorityQueue is not synchronized. BlockingPriorityQueue is the threadsafe implementation.
4. What is difference between dequeue() and peek() function of java?
a) dequeue() and peek() remove and return the next time in line
b) dequeue() and peek() return the next item in line
c) dequeue() removes and returns the next item in line while peek() returns the next item in line
d) peek() removes and returns the next item in line while dequeue() returns the next item in line
Answer
Answer: c [Reason:] dequeue() removes the item next in line. peek() returns the item without removing it from the queue.
5. What is the difference between Queue and Stack?
a) Stack is LIFO; Queue is FIFO
b) Queue is LIFO; Stack is FIFO
c) Stack and Queue is FIFO
d) Stack and Queue is LIFO
Answer
Answer: a [Reason:] Stack is Last in First out (LIFO) and Queue is First in First out(FIFO).
6. What is the use of front and rear pointers in CircularQueue implementation?
a) Front pointer points to first element; rear pointer points to the last element
b) Rear pointer points to first element; front pointer points to the last element
c) Front and read pointers point to the first element
d) Front pointer points to the first element; rear pointer points to null object
Answer
Answer: c [Reason:] CircularQueue implementation is an abstract class where first and rear pointer point to the same object.
7. What is the correct method used to insert and delete items from queue?
a) push and pop
b) enqueue and dequeue
c) enqueue and peek
d) add and remove
Answer
Answer: b [Reason:] enqueue is pushing item into queue; dequeue is removing item from queue; peek returns object without removing it from queue.
Stack uses push and pop methods. add and remove are used in list.
8. Which data structure is used in Breadth First Traversal of a graph?
a) Stack
b) Queue
c) Array
d) Tree
Answer
Answer: b [Reason:] In Breadth First Traversal of graph the nodes at the same level are accessed in the order of retrieval (i.e FIFO).
9. Where does a new element be inserted in linked list implementation of a queue?
a) Head of list
b) Tail of list
c) At the centre of list
d) All the old entries are pushed and then the new element is inserted
Answer
Answer: b [Reason:] To maintain FIFO, newer elements are inserted to the tail of the list.
10. If the size of the array used to implement circular queue is MAX_SIZE. How rear moves to traverse inorder to insert an element in the queue?
a) rear=(rear%1)+MAX_SIZE
b) rear=(rear+1)%MAX_SIZE
c) rear=rear+(1%MAX_SIZE)
d) rear=rear%(MAX_SIZE+1)
Answer
Answer: b [Reason:] The front and rear pointer od circular queue point to the first element.
Java MCQ Set 3
1. Which of these events is generated when the a window is closed?
a) TextEvent
b) MouseEvent
c) FocusEvent
d) WindowEvent
Answer
Answer: d [Reason:] A WindowEvent is generated when a window is opened, close, activated or deactivated.
2. Which of these events is generated when the component is added or removed?
a) ComponentEvent
b) ContainerEvent
c) FocusEvent
d) InputEvent
Answer
Answer: b [Reason:] A ContainerEvent is generated when a component is added to or removed from a container. It has two integer constants COMPONENT_ADDED & COMPONENT_REMOVED.
3. Which of these methods can be used to obtain the coordinates of a mouse?
a) getPoint()
b) getCoordinates()
c) getMouseXY()
d) getMouseCordinates()
Answer
Answer: a [Reason:] getPoint() method can be used to obtain coordinates of a mouse, alternatively we can use getX() and getY() methods for x and y coordinates of mouse respectively.
4. Which of these methods can be used to change location of an event?
a) ChangePoint()
b) TranslatePoint()
c) ChangeCordinates()
d) TranslateCordinates()
Answer
Answer: b [Reason:] None.
5. Which of these are integer constants of TextEvent class?
a) TEXT_CHANGED
b) TEXT_FORMAT_CHANGED
c) TEXT_VALUE_CHANGED
d) TEXT_sIZE_CHANGED
Answer
Answer: c [Reason:] TextEvent defines a single integer constant TEXT_VALUE_CHANGED.
6. Which of these methods is used to obtain the object that generated a WindowEvent?
a) getMethod()
b) getWindow()
c) getWindowEvent()
d) getWindowObject()
Answer
Answer: b [Reason:] None.
7. MouseEvent is subclass of which of these classes?
a) ComponentEvent
b) ContainerEvent
c) ItemEvent
d) InputEvent
Answer
Answer: d [Reason:] None.
8. Which of these methods is used to get x coordinate of the mouse?
a) getX()
b) getXCoordinate()
c) getCoordinateX()
d) getPointX()
Answer
Answer: a [Reason:] getX() and getY() are used to obtain X AND Y coordinates of the mouse.
9. Which of these are constants defined in WindowEvent class?
a) WINDOW_ACTIVATED
b) WINDOW_CLOSED
c) WINDOW_DEICONIFIED
d) All of the mentioned
Answer
Answer: d [Reason:] WindowEvent class defines 7 constants – WINDOW_ACTIVATED, WINDOW_CLOSED, WINDOW_OPENED, WINDOW_DECONIFIED, WINDOW_CLOSING, WINDOW_DEACTIVATED, WINDOW_ICONIFIED.
10. Which of these is superclass of WindowEvent class?
a) WindowEvent
b) ComponentEvent
c) ItemEvent
d) InputEvent
Answer
Answer: b [Reason:] ComponentEvent is superclass of ContainerEvent, FocusEvent, KeyEvent, MouseEvent and WindowEvent.
Java MCQ Set 4
1. What does Liskov substitution principle specify?
a) parent class can be substituted by child class
b) child class can be substituted by parent class
c) parent class cannot be substituted by child class
d) No classes can be replaced by each other
Answer
Answer: a [Reason:] Liskov substitution principle states that Objects in a program should be replaceable with instances of their sub types without altering the correctness of that program.
2. What is the correct option with regards to below snippet?
-
interface ICust
-
{
-
}
-
class RegularCustomer implements ICust
-
{
-
}
-
class OneTimeCustomer implements ICust
-
{
-
}
a) ICust can be replaced with RegularCustomer
b) RegularCustomer can be replaced with OneTimeCustomer
c) OneTimeCustomer can ve replaced with RegularCustomer
d) We can instantiate objects of ICust
Answer
Answer: a [Reason:] According to Liskov substitution principle we can replace ICust with RegularCustomer or OneTimeCustomer without affecting functionality.
-
public class Shape
-
{
-
public int area()
-
{
-
return 1;
-
}
-
}
-
public class Square extends Shape
-
{
-
public int area()
-
{
-
return 2;
-
}
-
}
-
public class Rectangle extends Shape
-
{
-
public int area()
-
{
-
return 3;
-
}
-
}
3. What is the outcome of below Main class?
-
class Main()
-
{
-
public static void main(String[] args)
-
{
-
Shape shape = new Shape();
-
Square square = new Square();
-
shape = square;
-
System.out.println(shape.area());
-
}
-
}
a) Compilation failure
b) Runtime failure
c) 1
d) 2
Answer
Answer: d [Reason:] Child object can be assigned to parent variable without change in behaviour.
4. What is the outcome of below code Main class?
-
class Main()
-
{
-
public static void main(String[] args)
-
{
-
Shape shape = new Shape();
-
Rectangle square = new Rectangle();
-
shape = square;
-
System.out.println(shape.area());
-
}
-
}
a) Compilation failure
b) 3
c) 1
d) 2
Answer
Answer: b [Reason:] Child object can be assigned to parent variable without change in behaviour.
5. What is the outcome of below code Main class?
-
class Main()
-
{
-
public static void main(String[] args)
-
{
-
Shape shape = new Shape();
-
Square square = new Square();
-
square = shape;
-
System.out.println(square.area());
-
}
-
}
a) Compilation failure
b) 3
c) 1
d) 2
Answer
Answer: a [Reason:] Parent object cannot be assigned to child class.
6. What is the outcome of below code Main class?
-
class Main()
-
{
-
public static void main(String[] args)
-
{
-
Shape shape = new Shape();
-
Square square = new Square();
-
Rectangle rect = new Rectangle();
-
rect = (Rectangle)shape;
-
System.out.println(square.area());
-
}
-
}
a) Compilation failure
b) 3
c) Runtime Exception
d) 2
Answer
Answer: c [Reason:] ClassCastException is thrown as we cannot assign parent object to child variable.
7. What is the outcome of below code Main class?
-
class Main()
-
{
-
public static void main(String[] args)
-
{
-
Shape shape = new Shape();
-
Square square = new Square();
-
Rectangle rect = new Rectangle();
-
rect = (Rectangle)square;
-
System.out.println(square.area());
-
}
-
}
a) Compilation failure
b) 3
c) Runtime Exception
d) 2
Answer
Answer: a [Reason:] We cannot assign one child class object to another child class variable.
-
interface Shape
-
{
-
public int area();
-
}
-
public class Square implements Shape
-
{
-
public int area()
-
{
-
return 2;
-
}
-
}
-
public class Rectangle implements Shape
-
{
-
public int area()
-
{
-
return 3;
-
}
-
}
8. What is the outcome of below code Main class?
-
class Main()
-
{
-
public static void main(String[] args)
-
{
-
Shape shape = new Shape();
-
Square square = new Square();
-
Rectangle rect = new Rectangle();
-
rect = (Rectangle)square;
-
System.out.println(square.area());
-
}
-
}
a) Compilation failure
b) 3
c) Runtime Exception
d) 2
Answer
Answer: a [Reason:] Interface cannot be instantiated. So we cannot create instances of shape.
9. What is the outcome of below code Main class?
-
public static void main(String[] args)
-
{
-
Shape shape = new Square();
-
shape = new Rectangle();
-
System.out.println(shape.area());
-
}
a) Compilation failure
b) 3
c) Runtime Exception
d) 2
Answer
Answer: b [Reason:] With parent class variable we can access methods declared in parent class. If the parent class variable is assigned child class object than it accesses the method of child class.
10. What is the outcome of below code Main class?
-
public static void main(String[] args)
-
{
-
Shape square = new Square();
-
Shape rect = new Rectangle();
-
square = rect;
-
System.out.println(square.area());
-
}
a) Compilation failure
b) 3
c) Runtime Exception
d) 2
Answer
Answer: b [Reason:] The method of the child class object is accessed. When we reassign objects, the methods of the latest assigned object are accessed.
Java MCQ Set 5
1. Which of the following is not OOPS concept in Java?
a) Inheritance
b) Encapsulation
c) Polymorphism
d) Compilation
Answer
Answer: d [Reason:] There are 4 OOPS concepts in Java. Inheritance, Encapsulation, Polymorphism and Abstraction.
2. Which of the following is a type of polymorphism in Java?
a) Compile time polymorphism
b) Execution time polymorphism
c) Multiple polymorphism
d) Multilevel polymorphism
Answer
Answer: a [Reason:] There are two type of polymorphism in Java. Compile time polymorphism (overloading) and runtime polymorphism (overriding).
3. When does method overloading is determined?
a) At run time
b) At compile time
c) At coding time
d) At execution time
Answer
Answer: b [Reason:] Overloading is determined at compile time. Hence, it is also known as compile time polymorphism.
4. When Overloading does not occur?
a) More than one method with same name but different method signature and different number or type of parameters
b) More than one method with same name, same signature but different number of signature
c) More than one method with same name, same signature, same number of parameters but different type
d) More than one method with same name, same number of parameters and type but different signature
Answer
Answer: d [Reason:] Overloading occurs when more than one method with same name but different constructor and also when same signature but different number of parameters and/or parameter type.
5. Which concept of Java is a way of converting real world objects in terms of class?
a) Polymorphism
b) Encapsulation
c) Abstraction
d) Inheritance
Answer
Answer: c [Reason:] Abstraction is concept of defining real world objects in terms of classes or interfaces.
6. Which concept of Java is achieved by combining methods and attribute into a class?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstration
Answer
Answer: a [Reason:] Encapsulation is implemented by combining methods and attribute into a class. The class acts like a container of encapsulating properties.
7. What is it called if an object has its own lifecycle and there is no owner?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
Answer
Answer: d [Reason:] It is a relationship where all objects have their own lifecycle and there is no owner. This occurs where many to many relationship is available, instead of one to one or one to many.
8. What is it called where child object gets killed if parent object is killed?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
Answer
Answer: b [Reason:] Composition occurs when child object gets killed if parent object gets killed. Aggregation is also known as strong Aggregation.
9. What is it called where object has its own lifecycle and child object cannot belong to another parent object?
a) Aggregation
b) Compostion
c) Encapsulation
d) Association
Answer
Answer: a [Reason:] Aggregation occurs when objects have their own life cycle and child object can associate with only one parent object.
10. Method overriding is combination of inheritance and polymorphism?
a) True
b) false
Answer
Answer: a [Reason:] In order for method overriding, method with same signation in both superclass and subclass is required with same signature. That satisfies both concepts inheritance and polymorphism.