Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Quiz 1 (A–D) — Worksheet Questions

Total questions: 133

Worksheet time: 1hrs 7mins

Name
Class
Date
1.

Which of the following allows a subclass to access a superclass method even when the subclass has overridden the superclass method?

a)

this.

b)

base.

c)

Super.

d)

public.

2.

Which of the following class members should usually be private?

a)

Constructors

b)

All other options are correct

c)

Variables (or fields).

d)

Methods

3.

Which of the following is false?

a)

A static method must be used to access private static instance variables

b)

A static method can be accessed even when no objects of its class have been instantiated

c)

A static method has no this reference

d)

A static method can call instance methods directly

4.

When must a program explicitly use the this reference?

a)

When accessing a private variable

b)

When accessing an instance variable that is shadowed by a local variable

c)

When accessing a local variable

d)

When accessing a public variable

5.

Static class variables: ..............

a)

are final

b)

are private

c)

are shared by all objects of a class

d)

are public

6.

Inheritance is also known as the ..............

a)

Know-a relationship

b)

Uses-a relationship

c)

Has-a relationship

d)

Is-a relationship

7.

Using public set methods helps provide data integrity if: .......

a)

The instance variables are private.

b)

The instance variables are private, or methods perform validity checking.

c)

The methods perform validity checking.

d)

The instance variables are public.

8.

A final field should also be declared if it is initialized in its declaration

a)

static.

b)

private.

c)

protected.

d)

public.

9.

A constructor cannot...

a)

have the same name as the class.

b)

initialize variables to their defaults.

c)

be overloaded.

d)

specify return types or return values.

10.

Which of the following statements is true?

a)

Methods and instance variables can both be either public or private

b)

Information hiding is achieved by restricting access to class members via keyword public.

c)

The private members of a class are directly accessible to the clients of a class.

d)

None of the other options is true.

11.

If a method is private, it can be accessed from outside the class directly.

a)

True

b)

False

12.

A class constructor must have a return type.

a)

True

b)

False

13.

The "static" modifier in Java allows methods and attributes to be accessed without creating an instance of the class.

a)

True

b)

False

14.

Overloading a method in Java means creating multiple methods with the same name but different parameter lists.

a)

True

b)

False

15.

What is the main goal of encapsulation in object-oriented programming?

a)

To hide "sensitive" data from users

b)

To increase the speed of data processing

c)

To provide a default access level for attributes

d)

To improve program readability

16.

The Object class in Java is the default superclass of all classes if no other superclass is specified.

a)

True

b)

False

17.

The keyword this is used to refer to the current instance of a class.

a)

True

b)

False

18.

Method overloading allows multiple methods to have the same name but different parameters in the same class.

a)

True

b)

False

19.

Which of the following methods can access private attributes directly?

a)

Only protected methods

b)

Only public methods

c)

Any method in the same package

d)

Only private methods within the same class

20.

What access level does the "protected" modifier grant to a class member?

a)

Accessible only within the same class

b)

Accessible to all classes everywhere

c)

Accessible only within its own package

d)

Accessible in the same package and by subclasses

21.

Which of the following class definitions defines a legal abstract class?

a)

public class abstract A { abstract void unfinished(); }

b)

class A { abstract void unfinished(); }

c)

class A { abstract void unfinished() { } }

d)

abstract class A { abstract void unfinished(); }

22.

Which of the following statements regarding abstract methods is false?

a)

A subclass can override a concrete method in a superclass to declare it abstract.

b)

An abstract class can be used as a data type.

c)

An abstract class can have instances created using the constructor of the abstract class.

d)

An abstract class can be extended.

e)

A subclass of a non-abstract superclass can be abstract.

23.

Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a no-arg constructor. Which of the following is correct?

a)

A a = new A();

b)

No answer is correct

c)

A a = new B();

d)

B b = new A();

24.

Which of the following declares an abstract method in an abstract Java class?

a)

public abstract void method();

b)

public abstract method();

c)

public void abstract method();

d)

public void method() {}

e)

public abstract void method() {}

25.

Which of the following statements regarding abstract methods is false?

a)

It is possible to define an abstract class that contains no abstract methods.

b)

A data field can be declared abstract.

c)

A class that contains abstract methods must be abstract.

d)

An abstract method cannot be contained in a nonabstract class.

e)

) Abstract classes have constructors.

26.

Instances of ________ are unchecked exceptions.

a)

RuntimeException, and Exception

b)

Exception, and Error

c)

Exception

d)

RuntimeException, Error, and NumberFormatException

e)

Throwable

27.

A Java exception is an instance of ________.

a)

Exception

b)

RuntimeException

c)

Error

d)

NumberFormatException

e)

Throwable

28.

What exception type does the following program throw? public class Test { public static void main(String[] args) { int[] list = new int[5]; System.out.println(list[5]); } }

a)

ArrayIndexOutOfBoundsException

b)

ArithmeticException

c)

ClassCastException

d)

StringIndexOutOfBoundsException

29.

What is the output of running class Test? public class Test { public static void main(String[] args) { new Circle9(); } } public abstract class GeometricObject { protected GeometricObject() { System.out.print("A"); } protected GeometricObject(String color, boolean filled) { System.out.print("B"); } } public class Circle9 extends GeometricObject { /** No-arg constructor */ public Circle9() { this(10); System.out.print("C"); } /** Construct circle with a specified radius */ public Circle9(double radius) { this(radius, "white", false); System.out.print("D"); } /** Construct a circle with specified radius, filled, and color */ public Circle9(double radius, String color, boolean filled) { super(color, filled); System.out.print("E"); } }

a)

ABCD

b)

CBAE

c)

BEDC

d)

BACD

e)

AEDC

30.

An instance of ________ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully.

a)

Error

b)

Exception

c)

NumberFormatException

d)

Throwable

e)

RuntimeException

31.

If a class contains at least one abstract method, then it is an abstract class.

a)

True

b)

False

32.

A “has-a” relationship is implemented via inheritance.

a)

True

b)

False

33.

A superclass’s ____________ members can be accessed in the superclass declaration and in subclass declarations.

a)

public and default

b)

private and default

c)

public and protected

d)

public and private

34.

Which of the following is a valid method signature?

a)

void abc(String a, String b)

b)

abc(String a, String b)

c)

public void abc(String a, String b)

d)

void abc(String a, String b{

35.

______________ involves using a superclass variable to invoke methods on superclass and subclass objects, enabling you to develop generic programs.

a)

Inheritance

b)

Overloading

c)

Polymorphism

d)

Overriding

36.

We can create a “Car” class that has an “is-a” relationship with the “SteeringWheel” and “Brakes” classes.

a)

True

b)

False

37.

Methods that are not interface methods and that do not provide implementations must be declared ______________

a)

Private

b)

Protected

c)

public

d)

abstract

38.

________ is a form of software reusability in which new classes acquire the members of existing classes and embellish those classes with new capabilities.

a)

Encapsulation

b)

Polymorphism

c)

Abstraction

d)

Inheritance

39.

Given the following code in a try block. int arr[ ] = new int[7]; arr[10] = 10 / 0; What do you expect to happen if the following catch blocks are included catch(ArrayIndexOutOfBoundsException e){ System.out.println("Out of boundary"); } catch(ArithmeticException e){ System.out.println("Divide by zero"); } catch(Exception e){ System.out.println("Some Other Exception"); }

a)

Out of boundary Divide by zero Some Other Exception

b)

Out of boundary

c)

Out of boundary Divide by zero

d)

Divide by zero

40.

All methods in an abstract class must be declared as abstract methods.

a)

True

b)

False

41.

Which term describes an entity that has different meanings?

a)

Super entity

b)

Parent entity

c)

Polymorphic entity

d)

Subclass entity

42.

A subclass cannot inherit which members of its superclass?

a)

private

b)

public

c)

protected

d)

default

43.

To make an attribute read-only, which approach should be implemented?

a)

only set method

b)

only get method

c)

only constructor

d)

a and b

44.

What is the output of this code? package radio; public class Song { public void playMusic() { System.out.print("Play!"); } } private static int playMusic() { System.out.print(" Music!"); } public static void main(String[] tracks) { new Song().playMusic(); }

a)

Play!

b)

Music!

c)

The code does not compile.

d)

The code compiles but the answer cannot be determined until runtime.

45.

With which modifier is code accessible in the same package and in subclasses?

a)

private

b)

final

c)

public

d)

protected

46.

Given the following code: class Employee { private void myname() { System.out.println("I am an Employee"); } public static void main(String[] args) { Employee.myname(); } } What is the output of this code?

a)

I am an Employee

b)

error: the method myname() is not a static method

c)

error, because we did not use inheritance

d)

error, because myname() is private

47.

Given the following code: public class Vehicle { public void move() { System.out.println("The vehicle moves"); } } public class Car extends Vehicle { public void move() { System.out.println("The car moves"); } } public class Main { public static void main(String[] args) { Vehicle vehicle = new Car(); vehicle.move(); } } The output of this code is...

a)

The vehicle moves

b)

The code doesn't compile

c)

The car moves

d)

Runtime error

48.

Is it possible to override a static method?

a)

True

b)

False

49.

Is it possible to overload a static method?

a)

True

b)

False

50.

The new keyword creates objects at compile time.

a)

True

b)

False

51.

Given the following class description, select the correct statement for implementing getter and setter for the age attribute such that age must be within the range of 20 to 40 inclusive. public class Student { private String name; private int age; } Which implementation requirement is correct?

a)

setAge accepts any integer; getAge returns age

b)

setAge assigns values only when age is between 20 and 40 inclusive; otherwise it ignores or rejects the input; getAge returns age

c)

getAge sets the age and setAge returns the current age

d)

setAge allows only values strictly between 21 and 39; getAge returns age

52.

Given the following code, what change is needed to properly override the watch() method? package entertainment; class Television { protected final void watch() {} } public class LCD extends Television { Object watch() {} }

a)

Remove final from Television.watch() and use the same signature (protected void watch()) in LCD

b)

Change LCD.watch() return type to Object and keep final in Television

c)

Mark LCD.watch() as static

d)

Make Television.watch() private instead of protected

53.

Object-oriented programming allows you to derive new classes from existing classes. This is called .............

a)

encapsulation

b)

inheritance

c)

abstraction

d)

generalization

54.

Which of the following statements is true?

a)

A subclass is a subset of a superclass.

b)

"class A extends B" means B is a subclass of A.

c)

A subclass is usually extended to contain more functions and more detailed information than its superclass.

d)

A superclass is usually extended to contain more functions and more detailed information than its superclass.

55.

What is the output of running class C? class A { public A() { System.out.println("A Constructor"); } } class B extends A { public B() { System.out.println("B Constructor"); } } public class C { public static void main(String[] args) { B b = new B(); } }

a)

Nothing displayed

b)

A Constructor

c)

B Constructor

d)

A Constructor B Constructor

56.

Which of the following is incorrect?

a)

A constructor may be static.

b)

A constructor may be private.

c)

A constructor may invoke a static method.

d)

A constructor may invoke an overloaded constructor.

57.

Which of the statements regarding the super keyword is incorrect?

a)

You can use super to invoke a super class constructor.

b)

You can use super to invoke a super class method.

c)

You can use super.super.p to invoke a method in superclass's parent class.

d)

You cannot invoke a method in superclass's parent class.

58.

Which of the following statements is not true?

a)

A method can be overloaded in the same class.

b)

A method can be overridden in the same class.

c)

If a method overrides another method, these two methods must have the same signature.

d)

A method in a subclass can overload a method in the superclass.

59.

Which of the following implements polymorphism?

a)

data fields should be declared private

b)

a class can extend another class

c)

variable of supertype can refer to a subtype object

d)

a class can contain another class

60.

public class Test { public static void main(String[] args) { String s = new String("Welcome to EELU"); Object o = s; String d = (String) o; } } Which statement is correct?

a)

When assigning s to o in Object o = s, a new object is created.

b)

When casting o in String d = (String)o, a new object is created.

c)

When casting o in String d = (String)o, the contents of o is changed.

d)

s, o, and d reference the same String object.

61.

What is the output of the following code? public class Test { public static void main(String[] args) { String s1 = new String("Java"); String s2 = new String("Java"); System.out.print((s1 == s2) + " " + (s1.equals(s2))); } }

a)

false false

b)

true true

c)

false true

d)

true false

62.

Given two reference variables t1 and t2, if t1==t2 is true. Then .............

a)

t1.equals(t2) can be false

b)

t1.equals(t2) must be true

c)

t1.equals(t2) may not be true

d)

None of the answers is correct

63.

What will happen when we compile and run the following code? class Computer { protected int process() { return 5; } } class Laptop extends Computer { public int process() { return 3; } } public static void main(String[] chips) { System.out.print(new Laptop().process()); }

a)

It prints 3.

b)

It prints 5.

c)

Compilation error due to access modifier.

d)

Runtime error due to method overriding.

64.

For the following Java class, what is printed when the program runs? public class Test { private static int instanceCtr = 0; public Test() { instanceCtr++; } public static int getInstanceCount() { return instanceCtr; } public static void main(String[] args) { Test obj1 = new Test(); Test obj2 = new Test(); Test obj3 = new Test(); System.out.println("Number of instances created: " + Test.getInstanceCount()); } }

a)

Number of instances created: 0

b)

Number of instances created: 1

c)

Number of instances created: 2

d)

Number of instances created: 3

65.

What is the output of the following application? public class Baseball { public static void main(String... teams) { try { int score = 1; System.out.print(score++); } catch (Throwable t) { System.out.print(score++); } finally { System.out.print(score++); } System.out.print(score++); } }

a)

123

b)

124

c)

12

d)

None of these options

66.

Which of the following is a checked exception?

a)

ClassCastException

b)

IOException

c)

ArrayIndexOutOfBoundsException

d)

IllegalArgumentException

67.

Which of the following statements about a finally block is true?

a)

Every line of the finally block is guaranteed to be executed.

b)

The finally block is executed only if the related catch block is also executed.

c)

The finally statement requires brackets {}.

d)

The finally block cannot throw an exception.

68.

Which statement about the role of exceptions in Java is incorrect?

a)

Exceptions are often used when things "go wrong" or deviate from the expected path.

b)

An application that throws an exception will terminate.

c)

Some exceptions can be avoided programmatically.

d)

An application that can properly handle its exception may recover from unexpected problems.

69.

If an exception matches two or more catch blocks, which catch block is executed?

a)

The first one that matches is executed.

b)

The last one that matches is executed.

c)

All matched blocks are executed.

d)

It is not possible to write code like this.

70.

Which modifier can be applied to an abstract interface method?

a)

protected

b)

static

c)

final

d)

public

71.

Which of the following statements about interfaces is not true?

a)

An interface can extend another interface.

b)

An interface can implement another interface.

c)

A class can implement two interfaces.

d)

A class can extend another class.

72.

Excluding default and static methods, .......... can contain both abstract and concrete methods, while .......... contains only abstract methods.

a)

concrete class, abstract class

b)

concrete class, interface

c)

interface, abstract class

d)

abstract class, interface

73.

What will the output of the provided abstract class and subclass implementation be? abstract class Car { static {System.out.print("1")} public Car(String name) { super(); System.out.print("2"); } { System.out.print("3"); } } public class BlueCar extends Car { { System.out.print("4"); } public BlueCar() { super("blue"); System.out.print("5"); } public static void main(String[] gears) { new BlueCar(); } }

a)

23451

b)

12354

c)

13245

d)

The code does not compile

74.

A Java exception is an instance of ..........

a)

RuntimeException

b)

Exception

c)

Error

d)

Throwable

75.

The code that sets the size of the button btn to 30 pixels height and 60 width and position at pixels 30 from the left and pixels 60 from the top of the container is ........

a)

btn.setBounds(30,60,60,30)

b)

btn.setBounds(60,60,60,30)

c)

btn.setBounds(30,60,30,60)

d)

btn.setBounds(60,30,30,60)

76.

What is the output of the following application involving static blocks and initialization? class A { static int i = 10; static { System.out.println(1); i = 100; } } public class StatInitializationBlock{ static { System.out.println(2); } public static void main(String [] args){ System.out.println(3); System.out.println(A.i); } }

a)

2 3 1 100

b)

2 3 1 10

c)

3 10 1 2

d)

The code does not compile

77.

Which of the following are primitives? int[] lowercase = new int[0]; Integer[] uppercase = new Integer[0];

a)

Only lowercase

b)

Only uppercase

c)

Both lowercase and uppercase

d)

Neither lowercase nor

78.

Which of the following is the proper code to determine the number of elements in an array?

a)

buses.length

b)

buses.length()

c)

buses.size

d)

buses.size()

79.

Which of the following create an empty two-dimensional array with dimensions 2x2?

a)

int [][] blue = new int [2, 2]

b)

int [][] blue = new int [2], [2]

c)

int [][] blue = new int[2][2]

d)

int [][] blue = new int[2 x 2]

80.

The Scanner class method used for reading a line of text is ........

a)

myReader.readLine()

b)

myReader.nextLine()

c)

myReader.readNextLine()

d)

myReader.scanLine()

81.

Which of the following is the right syntax of defining a file handle in Java?

a)

File myObj = new File(new Object())

b)

File myObj = new File("c:\filename.txt")

c)

File myObj = new File("c:\\filename.txt")

d)

File myObj = new new File(c:\filename.txt)

82.

Java AWT components are platform-dependent.

a)

True

b)

False

83.

Java Swing components have different look and feel for the different platforms.

a)

True

b)

False

84.

The Container is a component in AWT that can contain other containers.

a)

True

b)

False

85.

The java.awt.event package provides many event listener classes for event handling.

a)

True

b)

False

86.

The event object only carries the information about the event occurred.

a)

True

b)

False

87.

We create event listener class by extending ActionListener class.

a)

True

b)

False

88.

FlowLayout is the default layout manager for every java JFrame.

a)

True

b)

False

89.

FlowLayout is the default layout manager for every JPanel.

a)

True

b)

False

90.

LinkedList is a data structure that does not need shift in insertion operation.

a)

True

b)

False

91.

Queue is the class that implements last-in-first-out data structure.

a)

True

b)

False

92.

Stack is the class that implements first-in-first-out data structure.

a)

True

b)

False

93.

ArrayList allows random access of possibly duplicated elements.

a)

True

b)

False

94.

The elements in TreeSet are stored in ascending order.

a)

True

b)

False

95.

Polymorphism means writing code that will work for types not known at the time of writing the code.

a)

True

b)

False

96.

Overloading is a mechanism that is considered as a type of inclusion polymorphism.

a)

True

b)

False

97.

Abstraction is a process of hiding the functionality details and showing only implementation to the user.

a)

True

b)

False

98.

To extend an abstract class we must override all its methods.

a)

True

b)

False

99.

To implement method overloading, the method declared in child class must be more restrictive.

a)

True

b)

False

100.

The java final keyword is used to restrict the use of variables, methods, and classes.

a)

True

b)

False

101.

In Java, input streams are only used to read data from a data source.

a)

True

b)

False

102.

All collection classes implement the Iterable interface.

a)

True

b)

False

103.

The method put() is the one that puts a new item on top of a stack.

a)

True

b)

False

104.

Set is an ordered collection that doesn’t allow storing duplicate items.

a)

True

b)

False

105.

The elements in TreeSet collection are stored in ascending order.

a)

True

b)

False

106.

Which of the following statements about a finally block is true?

a)

Every line of the finally block is guaranteed to be executed.

b)

The finally block is executed only if catch block is also executed.

c)

The finally statement requires brackets {}.

d)

The finally block cannot throw an exception.

107.

Exception Handling in Java handles ............

a)

Compilation errors

b)

run-time errors

c)

both compilation and run-time errors

d)

Syntax errors

108.

Which of the following exception types is a checked exception?

a)

this

b)

IOException

c)

final

d)

static

109.

If an exception matches two or more catch blocks, which catch block is executed?

a)

The first one that matches is executed.

b)

The last one that matches is executed.

c)

All matched blocks are executed.

d)

It is not possible to write code like this

110.

Which of the following statements about interfaces is not true?

a)

An interface can extend another interface.

b)

An interface can implement another interface.

c)

A class can implement two interfaces.

d)

It is not possible to write code like this.

111.

A Java exception is an instance of

a)

Runtime Exception.

b)

Exception

c)

Error.

d)

Throwable.

112.

If the types for which a piece of code works are finite, then it is considered as an implementation of ............

a)

Universal polymorphism

b)

Inclusion polymorphism

c)

Ad-hoc polymorphism

d)

Parametric polymorphism

113.

Type of polymorphism that is achieved by using a type variable when writing the code rather than using any specific type is called ............

a)

Universal polymorphism

b)

Parametric polymorphism

c)

Inclusion polymorphism

d)

Ad-hoc polymorphism

114.

Which of the following is true?

a)

Abstract classes can be instantiated

b)

A inheritance can be instantiated

c)

Both Abstract classes and interfaces can not be instantiated

d)

Both Abstract classes and interfaces can be instantiated

115.

The process of determining the appropriate method or function to call at runtime, based on the actual type of the object rather than the reference type is called ...............

a)

Static binding

b)

Static referencing

c)

Dynamic referencing

d)

Dynamic binding

116.

Multiple inheritance can be achieved using .................

a)

Interfaces

b)

Abstract class

c)

Dynamic binding

d)

polymorphism

117.

Which of the following is false?

a)

Subclasses implement interfaces

b)

Subclasses can extend multiple abstract classes

c)

Subclasses can implement multiple interfaces

d)

Subclasses can extend other subclasses

118.

Which of the following code blocks that contains a code where an exception may occur

a)

try

b)

catch

c)

finally

d)

Static block

119.

An object that is notified by the event source when an event occurs is called ............

a)

Event Listener Object

b)

Event object

c)

GUI component

d)

Interface object

120.

Which of the following is true about a final method?

a)

It can be overridden but cannot be inherited

b)

It can be both overridden and inherited

c)

It cannot be overridden and cannot be inherited

d)

It cannot be overridden, but still can be inherited

121.

interfaces can implement other interfaces. Choose True or False.

a)

True

b)

False

122.

An abstract interface method can have a static modifier. Choose True or False.

a)

True

b)

False

123.

Polymorphism supports maintainability. Choose True or False.

a)

True

b)

False

124.

Abstract classes can have only abstract methods. Choose True or False.

a)

True

b)

False

125.

Any class which extends abstract class must override all its abstract methods. Choose True or False.

a)

True

b)

False

126.

Both an interface and its constructor must have the same name. Choose True or False.

a)

True

b)

False

127.

All fields in an interface are implicitly public, static, and final. Choose True or False.

a)

True

b)

False

128.

Interface methods are by default abstract and private. Choose True or False.

a)

True

b)

False

129.

Overloading is a Universal polymorphism. Choose True or False.

a)

True

b)

False

130.

final is not allowed for Constructors. Choose True or False.

a)

True

b)

False

131.

What is the output of the following application? package machines; class Computer { protected final int process() { return 5; } } public class Laptop extends Computer { public final int process() { return 3; } public static void main(String[] chips) { System.out.printf(new Laptop().process()); } } Select the correct output.

a)

The code does not compile

b)

3

c)

5

d)

A runtime exception occurs

132.

What is the output of the following application? class A { static int i; static { System.out.println(1); i = 100; } } public class StaticInitializationBlock { static { System.out.println(2); } public static void main(String[] args) { System.out.println(3); System.out.println(A.i); } } Select the sequence of lines printed, one per line.

a)

2, 3, 1, 100

b)

1, 2, 3, 100

c)

3, 2, 1, 100

d)

2, 1, 3, 100

133.

What is the output of the following application? public class A { { System.out.println("First"); } { System.out.println("Second"); } { System.out.println("Third"); } public A() { System.out.println("Fourth"); } public static void main(String[] args) { System.out.println("Fifth"); A a = new A(); } } Select the correct order of lines printed.

a)

Fifth, First, Second, Third, Fourth

b)

First, Second, Third, Fourth, Fifth

c)

Fifth, Fourth, First, Second, Third

d)

First, Third, Second, Fourth, Fifth