WorksheetsQuiz 1 (A–D) — Worksheet Questions
Total questions: 133
Worksheet time: 1hrs 7mins
Which of the following allows a subclass to access a superclass method even when the subclass has overridden the superclass method?
this.
base.
Super.
public.
Which of the following class members should usually be private?
Constructors
All other options are correct
Variables (or fields).
Methods
Which of the following is false?
A static method must be used to access private static instance variables
A static method can be accessed even when no objects of its class have been instantiated
A static method has no this reference
A static method can call instance methods directly
When must a program explicitly use the this reference?
When accessing a private variable
When accessing an instance variable that is shadowed by a local variable
When accessing a local variable
When accessing a public variable
Static class variables: ..............
are final
are private
are shared by all objects of a class
are public
Inheritance is also known as the ..............
Know-a relationship
Uses-a relationship
Has-a relationship
Is-a relationship
Using public set methods helps provide data integrity if: .......
The instance variables are private.
The instance variables are private, or methods perform validity checking.
The methods perform validity checking.
The instance variables are public.
A final field should also be declared if it is initialized in its declaration
static.
private.
protected.
public.
A constructor cannot...
have the same name as the class.
initialize variables to their defaults.
be overloaded.
specify return types or return values.
Which of the following statements is true?
Methods and instance variables can both be either public or private
Information hiding is achieved by restricting access to class members via keyword public.
The private members of a class are directly accessible to the clients of a class.
None of the other options is true.
If a method is private, it can be accessed from outside the class directly.
True
False
A class constructor must have a return type.
True
False
The "static" modifier in Java allows methods and attributes to be accessed without creating an instance of the class.
True
False
Overloading a method in Java means creating multiple methods with the same name but different parameter lists.
True
False
What is the main goal of encapsulation in object-oriented programming?
To hide "sensitive" data from users
To increase the speed of data processing
To provide a default access level for attributes
To improve program readability
The Object class in Java is the default superclass of all classes if no other superclass is specified.
True
False
The keyword this is used to refer to the current instance of a class.
True
False
Method overloading allows multiple methods to have the same name but different parameters in the same class.
True
False
Which of the following methods can access private attributes directly?
Only protected methods
Only public methods
Any method in the same package
Only private methods within the same class
What access level does the "protected" modifier grant to a class member?
Accessible only within the same class
Accessible to all classes everywhere
Accessible only within its own package
Accessible in the same package and by subclasses
Which of the following class definitions defines a legal abstract class?
public class abstract A { abstract void unfinished(); }
class A { abstract void unfinished(); }
class A { abstract void unfinished() { } }
abstract class A { abstract void unfinished(); }
Which of the following statements regarding abstract methods is false?
A subclass can override a concrete method in a superclass to declare it abstract.
An abstract class can be used as a data type.
An abstract class can have instances created using the constructor of the abstract class.
An abstract class can be extended.
A subclass of a non-abstract superclass can be abstract.
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 = new A();
No answer is correct
A a = new B();
B b = new A();
Which of the following declares an abstract method in an abstract Java class?
public abstract void method();
public abstract method();
public void abstract method();
public void method() {}
public abstract void method() {}
Which of the following statements regarding abstract methods is false?
It is possible to define an abstract class that contains no abstract methods.
A data field can be declared abstract.
A class that contains abstract methods must be abstract.
An abstract method cannot be contained in a nonabstract class.
) Abstract classes have constructors.
Instances of ________ are unchecked exceptions.
RuntimeException, and Exception
Exception, and Error
Exception
RuntimeException, Error, and NumberFormatException
Throwable
A Java exception is an instance of ________.
Exception
RuntimeException
Error
NumberFormatException
Throwable
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]); } }
ArrayIndexOutOfBoundsException
ArithmeticException
ClassCastException
StringIndexOutOfBoundsException
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"); } }
ABCD
CBAE
BEDC
BACD
AEDC
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.
Error
Exception
NumberFormatException
Throwable
RuntimeException
If a class contains at least one abstract method, then it is an abstract class.
True
False
A “has-a” relationship is implemented via inheritance.
True
False
A superclass’s ____________ members can be accessed in the superclass declaration and in subclass declarations.
public and default
private and default
public and protected
public and private
Which of the following is a valid method signature?
void abc(String a, String b)
abc(String a, String b)
public void abc(String a, String b)
void abc(String a, String b{
______________ involves using a superclass variable to invoke methods on superclass and subclass objects, enabling you to develop generic programs.
Inheritance
Overloading
Polymorphism
Overriding
We can create a “Car” class that has an “is-a” relationship with the “SteeringWheel” and “Brakes” classes.
True
False
Methods that are not interface methods and that do not provide implementations must be declared ______________
Private
Protected
public
abstract
________ is a form of software reusability in which new classes acquire the members of existing classes and embellish those classes with new capabilities.
Encapsulation
Polymorphism
Abstraction
Inheritance
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"); }
Out of boundary Divide by zero Some Other Exception
Out of boundary
Out of boundary Divide by zero
Divide by zero
All methods in an abstract class must be declared as abstract methods.
True
False
Which term describes an entity that has different meanings?
Super entity
Parent entity
Polymorphic entity
Subclass entity
A subclass cannot inherit which members of its superclass?
private
public
protected
default
To make an attribute read-only, which approach should be implemented?
only set method
only get method
only constructor
a and b
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(); }
Play!
Music!
The code does not compile.
The code compiles but the answer cannot be determined until runtime.
With which modifier is code accessible in the same package and in subclasses?
private
final
public
protected
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?
I am an Employee
error: the method myname() is not a static method
error, because we did not use inheritance
error, because myname() is private
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...
The vehicle moves
The code doesn't compile
The car moves
Runtime error
Is it possible to override a static method?
True
False
Is it possible to overload a static method?
True
False
The new keyword creates objects at compile time.
True
False
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?
setAge accepts any integer; getAge returns age
setAge assigns values only when age is between 20 and 40 inclusive; otherwise it ignores or rejects the input; getAge returns age
getAge sets the age and setAge returns the current age
setAge allows only values strictly between 21 and 39; getAge returns age
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() {} }
Remove final from Television.watch() and use the same signature (protected void watch()) in LCD
Change LCD.watch() return type to Object and keep final in Television
Mark LCD.watch() as static
Make Television.watch() private instead of protected
Object-oriented programming allows you to derive new classes from existing classes. This is called .............
encapsulation
inheritance
abstraction
generalization
Which of the following statements is true?
A subclass is a subset of a superclass.
"class A extends B" means B is a subclass of A.
A subclass is usually extended to contain more functions and more detailed information than its superclass.
A superclass is usually extended to contain more functions and more detailed information than its superclass.
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(); } }
Nothing displayed
A Constructor
B Constructor
A Constructor B Constructor
Which of the following is incorrect?
A constructor may be static.
A constructor may be private.
A constructor may invoke a static method.
A constructor may invoke an overloaded constructor.
Which of the statements regarding the super keyword is incorrect?
You can use super to invoke a super class constructor.
You can use super to invoke a super class method.
You can use super.super.p to invoke a method in superclass's parent class.
You cannot invoke a method in superclass's parent class.
Which of the following statements is not true?
A method can be overloaded in the same class.
A method can be overridden in the same class.
If a method overrides another method, these two methods must have the same signature.
A method in a subclass can overload a method in the superclass.
Which of the following implements polymorphism?
data fields should be declared private
a class can extend another class
variable of supertype can refer to a subtype object
a class can contain another class
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?
When assigning s to o in Object o = s, a new object is created.
When casting o in String d = (String)o, a new object is created.
When casting o in String d = (String)o, the contents of o is changed.
s, o, and d reference the same String object.
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))); } }
false false
true true
false true
true false
Given two reference variables t1 and t2, if t1==t2 is true. Then .............
t1.equals(t2) can be false
t1.equals(t2) must be true
t1.equals(t2) may not be true
None of the answers is correct
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()); }
It prints 3.
It prints 5.
Compilation error due to access modifier.
Runtime error due to method overriding.
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()); } }
Number of instances created: 0
Number of instances created: 1
Number of instances created: 2
Number of instances created: 3
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++); } }
123
124
12
None of these options
Which of the following is a checked exception?
ClassCastException
IOException
ArrayIndexOutOfBoundsException
IllegalArgumentException
Which of the following statements about a finally block is true?
Every line of the finally block is guaranteed to be executed.
The finally block is executed only if the related catch block is also executed.
The finally statement requires brackets {}.
The finally block cannot throw an exception.
Which statement about the role of exceptions in Java is incorrect?
Exceptions are often used when things "go wrong" or deviate from the expected path.
An application that throws an exception will terminate.
Some exceptions can be avoided programmatically.
An application that can properly handle its exception may recover from unexpected problems.
If an exception matches two or more catch blocks, which catch block is executed?
The first one that matches is executed.
The last one that matches is executed.
All matched blocks are executed.
It is not possible to write code like this.
Which modifier can be applied to an abstract interface method?
protected
static
final
public
Which of the following statements about interfaces is not true?
An interface can extend another interface.
An interface can implement another interface.
A class can implement two interfaces.
A class can extend another class.
Excluding default and static methods, .......... can contain both abstract and concrete methods, while .......... contains only abstract methods.
concrete class, abstract class
concrete class, interface
interface, abstract class
abstract class, interface
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(); } }
23451
12354
13245
The code does not compile
A Java exception is an instance of ..........
RuntimeException
Exception
Error
Throwable
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 ........
btn.setBounds(30,60,60,30)
btn.setBounds(60,60,60,30)
btn.setBounds(30,60,30,60)
btn.setBounds(60,30,30,60)
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); } }
2 3 1 100
2 3 1 10
3 10 1 2
The code does not compile
Which of the following are primitives? int[] lowercase = new int[0]; Integer[] uppercase = new Integer[0];
Only lowercase
Only uppercase
Both lowercase and uppercase
Neither lowercase nor
Which of the following is the proper code to determine the number of elements in an array?
buses.length
buses.length()
buses.size
buses.size()
Which of the following create an empty two-dimensional array with dimensions 2x2?
int [][] blue = new int [2, 2]
int [][] blue = new int [2], [2]
int [][] blue = new int[2][2]
int [][] blue = new int[2 x 2]
The Scanner class method used for reading a line of text is ........
myReader.readLine()
myReader.nextLine()
myReader.readNextLine()
myReader.scanLine()
Which of the following is the right syntax of defining a file handle in Java?
File myObj = new File(new Object())
File myObj = new File("c:\filename.txt")
File myObj = new File("c:\\filename.txt")
File myObj = new new File(c:\filename.txt)
Java AWT components are platform-dependent.
True
False
Java Swing components have different look and feel for the different platforms.
True
False
The Container is a component in AWT that can contain other containers.
True
False
The java.awt.event package provides many event listener classes for event handling.
True
False
The event object only carries the information about the event occurred.
True
False
We create event listener class by extending ActionListener class.
True
False
FlowLayout is the default layout manager for every java JFrame.
True
False
FlowLayout is the default layout manager for every JPanel.
True
False
LinkedList is a data structure that does not need shift in insertion operation.
True
False
Queue is the class that implements last-in-first-out data structure.
True
False
Stack is the class that implements first-in-first-out data structure.
True
False
ArrayList allows random access of possibly duplicated elements.
True
False
The elements in TreeSet are stored in ascending order.
True
False
Polymorphism means writing code that will work for types not known at the time of writing the code.
True
False
Overloading is a mechanism that is considered as a type of inclusion polymorphism.
True
False
Abstraction is a process of hiding the functionality details and showing only implementation to the user.
True
False
To extend an abstract class we must override all its methods.
True
False
To implement method overloading, the method declared in child class must be more restrictive.
True
False
The java final keyword is used to restrict the use of variables, methods, and classes.
True
False
In Java, input streams are only used to read data from a data source.
True
False
All collection classes implement the Iterable interface.
True
False
The method put() is the one that puts a new item on top of a stack.
True
False
Set is an ordered collection that doesn’t allow storing duplicate items.
True
False
The elements in TreeSet collection are stored in ascending order.
True
False
Which of the following statements about a finally block is true?
Every line of the finally block is guaranteed to be executed.
The finally block is executed only if catch block is also executed.
The finally statement requires brackets {}.
The finally block cannot throw an exception.
Exception Handling in Java handles ............
Compilation errors
run-time errors
both compilation and run-time errors
Syntax errors
Which of the following exception types is a checked exception?
this
IOException
final
static
If an exception matches two or more catch blocks, which catch block is executed?
The first one that matches is executed.
The last one that matches is executed.
All matched blocks are executed.
It is not possible to write code like this
Which of the following statements about interfaces is not true?
An interface can extend another interface.
An interface can implement another interface.
A class can implement two interfaces.
It is not possible to write code like this.
A Java exception is an instance of
Runtime Exception.
Exception
Error.
Throwable.
If the types for which a piece of code works are finite, then it is considered as an implementation of ............
Universal polymorphism
Inclusion polymorphism
Ad-hoc polymorphism
Parametric polymorphism
Type of polymorphism that is achieved by using a type variable when writing the code rather than using any specific type is called ............
Universal polymorphism
Parametric polymorphism
Inclusion polymorphism
Ad-hoc polymorphism
Which of the following is true?
Abstract classes can be instantiated
A inheritance can be instantiated
Both Abstract classes and interfaces can not be instantiated
Both Abstract classes and interfaces can be instantiated
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 ...............
Static binding
Static referencing
Dynamic referencing
Dynamic binding
Multiple inheritance can be achieved using .................
Interfaces
Abstract class
Dynamic binding
polymorphism
Which of the following is false?
Subclasses implement interfaces
Subclasses can extend multiple abstract classes
Subclasses can implement multiple interfaces
Subclasses can extend other subclasses
Which of the following code blocks that contains a code where an exception may occur
try
catch
finally
Static block
An object that is notified by the event source when an event occurs is called ............
Event Listener Object
Event object
GUI component
Interface object
Which of the following is true about a final method?
It can be overridden but cannot be inherited
It can be both overridden and inherited
It cannot be overridden and cannot be inherited
It cannot be overridden, but still can be inherited
interfaces can implement other interfaces. Choose True or False.
True
False
An abstract interface method can have a static modifier. Choose True or False.
True
False
Polymorphism supports maintainability. Choose True or False.
True
False
Abstract classes can have only abstract methods. Choose True or False.
True
False
Any class which extends abstract class must override all its abstract methods. Choose True or False.
True
False
Both an interface and its constructor must have the same name. Choose True or False.
True
False
All fields in an interface are implicitly public, static, and final. Choose True or False.
True
False
Interface methods are by default abstract and private. Choose True or False.
True
False
Overloading is a Universal polymorphism. Choose True or False.
True
False
final is not allowed for Constructors. Choose True or False.
True
False
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.
The code does not compile
3
5
A runtime exception occurs
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.
2, 3, 1, 100
1, 2, 3, 100
3, 2, 1, 100
2, 1, 3, 100
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.
Fifth, First, Second, Third, Fourth
First, Second, Third, Fourth, Fifth
Fifth, Fourth, First, Second, Third
First, Third, Second, Fourth, Fifth
