WorksheetsCP11-Q3
Total questions: 100
Worksheet time: 50mins
Name
Class
Date
1.
What does OOP stand for in programming?
a)
Output-oriented programming
b)
Online object programming
c)
Object-oriented programming
d)
Original object programming
2.
Which of the following is a key characteristic of object-oriented programming?
a)
Objects cannot store data
b)
It uses classes and objects to organize code
c)
It is based solely on functions
d)
It does not allow inheritance
3.
Which of the following concepts is NOT part of OOP?
a)
Encapsulation
b)
Polymorphism
c)
Inheritance
d)
Decoupling
4.
What is a class in Java?
a)
A blueprint or template from which objects are created
b)
A type of object
c)
A method for inheritance
d)
A collection of data without any functionality
5.
Which of the following is a correct example of an object in Java?
a)
"Dog" is a class, and "myDog" is an object
b)
"myDog" is a class, and "Dog" is an object
c)
"myDog" is a variable, and "Dog" is a method
d)
"Dog" is a function, and "myDog" is a method
6.
What is the purpose of the constructor in an object-oriented program?
a)
To destroy an object
b)
To define the methods in a class
c)
To initialize objects
d)
To store data
7.
Which concept in OOP allows different classes to share functionality by inheriting properties and methods from another class?
a)
Polymorphism
b)
Encapsulation
c)
Inheritance
d)
Abstraction
8.
Which of the following describes the concept of polymorphism in OOP?
a)
Hiding the internal details of an object
b)
Allowing one method to have different behaviors based on the object
c)
Combining data and functions into a single entity
d)
A way of managing access to data
9.
What is encapsulation in object-oriented programming?
a)
The process of combining multiple methods into one
b)
The practice of hiding data and allowing access through methods
c)
The ability to create multiple objects from a class
d)
The ability to define multiple functions with the same name
10.
In Java, how do you declare a class?
a)
public class ClassName { }
b)
class: ClassName { }
c)
class ClassName() { }
d)
public ClassName { }
11.
In object-oriented programming, what is an object?
a)
A class that contains only methods
b)
A specific instance of a class containing data and methods
c)
A program that performs calculations
d)
A function that manipulates data
12.
Which of the following is an example of inheritance in Java?
a)
A subclass inherits methods and properties from a superclass
b)
An object inherits properties from a method
c)
A class inherits only the properties from another class
d)
A class cannot inherit methods or properties from another class
13.
Which of the following is NOT a feature of object-oriented programming?
a)
Inheritance
b)
Classes and objects
c)
Methods without functions
d)
Encapsulation
14.
Which statement about constructors in Java is correct?
a)
A constructor is automatically called when an object is created
b)
A constructor has no return type
c)
A constructor must have a return type
d)
A constructor cannot initialize objects
Competency 2: Apply OOP principles in a Java program
15.
Which of the following is an example of applying inheritance in a Java program?
a)
Creating a subclass that inherits properties and methods from a superclass
b)
Defining a method in the superclass that can be overridden in the subclass
c)
Creating an object of a class using its constructor
d)
Writing a method that calculates the sum of two numbers
16.
What is the purpose of the super keyword in Java?
a)
It refers to the current instance of the class
b)
It is used to call methods and constructors from the superclass
c)
It helps create an object of a class
d)
It defines a constructor for the class
17.
Which principle of OOP allows an object to take on multiple forms based on the type of object being referenced?
a)
Polymorphism
b)
Inheritance
c)
Abstraction
d)
Encapsulation
18.
What is the main advantage of using encapsulation in a Java program?
a)
It allows data and methods to be hidden and protected from outside access
b)
It enables objects to inherit properties and methods from other classes
c)
It allows objects to take on multiple forms based on the class type
d)
It organizes classes into hierarchies
19.
In a Java program, how would you define a method that can be used by all instances of a class (i.e., a static method)?
a)
By using the static keyword in the method definition
b)
By using the final keyword in the method definition
c)
By using the abstract keyword in the method definition
d)
By defining the method inside the constructor
20.
How can you implement inheritance in Java?
a)
By creating a subclass and using the extends keyword to inherit from a superclass
b)
By defining the subclass inside the superclass
c)
By using the implements keyword
d)
By creating an object of the superclass inside the subclass
21.
What is the correct way to create an object of a class in Java?
a)
ClassName obj = new ClassName();
b)
obj = ClassName();
c)
new obj = ClassName();
d)
ClassName obj;
22.
What is the outcome of overriding a method in a subclass?
a)
The subclass method completely replaces the superclass method
b)
The subclass method cannot access the superclass method
c)
The superclass method is hidden from the subclass
d)
The subclass method can still call the superclass method using super
23.
How do you apply the concept of polymorphism in Java?
a)
By using inheritance to allow different classes to share the same method name but implement them differently
b)
By using super to call the superclass method
c)
By defining multiple constructors for a class
d)
By making a class abstract
24.
How do you make sure that data is encapsulated in a Java class?
a)
By using the private keyword to restrict access to class fields
b)
By using the public keyword for class fields
c)
By using inheritance
d)
By implementing methods to access and modify the fields directly
25.
When you want to define a method that can be overridden by subclasses, which keyword should be used in Java?
a)
abstract
b)
final
c)
static
d)
override
26.
Which of the following is an example of using an interface in Java?
a)
Defining a method in the class that can be implemented by other classes
b)
Defining a method in the superclass that can be inherited by subclasses
c)
Defining a set of abstract methods that can be implemented by a class
d)
Defining a class that can be used to create objects
27.
How do you achieve abstraction in Java?
a)
By using interfaces or abstract classes to hide implementation details
b)
By hiding all fields and methods inside the class
c)
By using the static keyword to make fields accessible without creating objects
d)
By using inheritance to share common behaviors across classes
28.
Which of the following would allow a class to access a private field of another class?
a)
Using a public method (getter/setter) to access the private field
b)
Making the field public
c)
Making the class static
d)
Making the field final
Competency 3: Apply exception handling in the Java program
29.
What is an exception in Java?
a)
A type of method used for handling errors
b)
An object that indicates an error or an unusual situation in the program
c)
A method that automatically stops the program
d)
A variable used to store error messages
30.
Which of the following keywords is used in Java to handle exceptions?
a)
try
b)
catch
c)
throw
d)
All of the above
31.
What is the purpose of the try block in Java?
a)
To catch exceptions
b)
To declare exceptions
c)
To define the block of code where exceptions might occur
d)
To handle exceptions after they occur
32.
In Java, what does the finally block do?
a)
It executes code only if an exception is thrown
b)
It always executes, regardless of whether an exception occurred or not
c)
It is used to declare exceptions
d)
It stops the program execution
33.
How would you handle an exception if you want to display a custom message when the exception occurs in Java?
a)
By using the throw keyword inside the catch block
b)
By writing a catch block that catches specific exceptions and prints a custom message
c)
By using the finally block to print a custom message
d)
By using the throw keyword outside of the try-catch block
34.
Which of the following is the correct way to write a try-catch block for handling a NullPointerException in Java?
a)
try {
// Code that might throw exception
} catch (NullPointerException e) {
System.out.println("Null pointer exception occurred!");
}
b)
try {
// Code that might throw exception
} catch (NullPointerException) {
System.out.println("Null pointer exception occurred!");
}
c)
try {
// Code that might throw exception
} finally {
System.out.println("Null pointer exception occurred!");
}
d)
try {
// Code that might throw exception
} throw NullPointerException;
35.
What will happen if an exception is not caught in a try-catch block in Java?
a)
The program will continue executing without interruption
b)
The exception will be thrown to the caller method or main method, potentially terminating the program
c)
The program will enter an infinite loop
d)
The program will exit without executing further code
36.
What is the difference between throw and throws in exception handling?
a)
throw is used to manually throw an exception, while throws is used to declare that a method may throw an exception
b)
throw is used to declare an exception, while throws is used to manually throw an exception
c)
There is no difference between throw and throws
d)
throw is used for checked exceptions, while throws is used for runtime exceptions
37.
How can you handle multiple exceptions in a single catch block in Java?
a)
By using multiple catch blocks for each exception type
b)
By using a single catch block and separating each exception type with a comma
c)
By using the | operator to separate the exception types in one catch block
d)
By using the throws keyword
38.
When handling exceptions in Java, which of the following should be avoided?
a)
Catching general Exception objects
b)
Catching specific exceptions
c)
Using multiple catch blocks for different exceptions
d)
Writing clean and descriptive error messages
39.
What is the best approach to catch and handle an exception when you don’t know the type of exception in advance?
a)
Use a general catch (Exception e) block to handle all exceptions
b)
Use catch (Throwable e) to handle all errors and exceptions
c)
Use multiple catch blocks for every possible exception
d)
Avoid handling exceptions and let the program terminate
40.
In Java, which of the following is the correct way to throw an exception manually?
a)
throw new Exception("This is an error");
b)
throws Exception("This is an error");
c)
catch new Exception("This is an error");
d)
throw Exception("This is an error");
41.
How can you specify that a method may throw an exception in Java?
a)
By using the throw keyword
b)
By using the throws keyword in the method signature
c)
By wrapping the method in a try-catch block
d)
By calling the catch block in the method
42.
Which of the following is the correct way to handle an exception and continue the program execution in Java?
a)
Use try-catch-finally where the finally block continues execution after an exception
b)
Use throw to exit the program
c)
Catch the exception but don’t print or log it
d)
Use catch only without a finally block
Competency 4: Apply Java Collections Framework in writing Java programs
43.
What is the primary purpose of the Java Collections Framework?
a)
To provide data structures and algorithms for efficient data manipulation
b)
To improve the performance of Java applications
c)
To manage memory efficiently in Java
d)
To store data in files and databases
44.
Which of the following is NOT part of the Java Collections Framework?
a)
List
b)
Set
c)
Map
d)
Array
45.
Which of the following classes implements the List interface in Java?
a)
ArrayList
b)
HashSet
c)
TreeMap
d)
PriorityQueue
46.
Which interface in Java is implemented by classes that store elements in a collection that does not allow duplicates?
a)
List
b)
Set
c)
Queue
d)
Map
47.
How would you create an ArrayList of Strings in Java?
a)
ArrayList<String> list = new ArrayList<String>();
b)
ArrayList<String> list = new ArrayList();
c)
List<String> list = new ArrayList();
d)
ArrayList<String> list = new List<String>();
48.
In the Java Collections Framework, which of the following methods would you use to add an element to a list?
a)
add()
b)
insert()
c)
put()
d)
append()
49.
What does the contains() method in a Set return in Java?
a)
True if the element is found in the set
b)
The index of the element in the set
c)
A boolean indicating if the set is empty
d)
The number of times the element appears in the set
50.
Which of the following statements is true about the HashSet class in Java?
a)
It maintains the order of elements
b)
It allows duplicate elements
c)
It does not allow duplicate elements
d)
It uses a list for its underlying implementation
51.
If you want to sort elements in a collection, which class or method should you use in Java?
a)
Collections.sort()
b)
TreeMap.sort()
c)
List.sort()
d)
Arrays.sort()
52.
How would you iterate over a HashMap to print each key-value pair in Java?
a)
By using a for loop on map.entrySet()
b)
By using a for loop on map.keys()
c)
By calling map.get() inside a for loop
d)
By using map.values() inside a for loop
53.
Which of the following is the correct way to access an element in a HashMap by key?
a)
hashMap.get(key)
b)
hashMap[key]
c)
hashMap.retrieve(key)
d)
hashMap.search(key)
54.
What will happen if you try to add a duplicate element to a HashSet in Java?
a)
The duplicate element will be added
b)
The duplicate element will be ignored
c)
A NullPointerException will be thrown
d)
The program will stop executing
55.
Which method would you use to remove an element from a List in Java?
a)
remove()
b)
delete()
c)
erase()
d)
discard()
56.
What is the correct way to convert an array to a List in Java?
a)
Arrays.asList(array)
b)
List.array()
c)
array.toList()
d)
array.convertToList()
Competency 5: Apply the java.io package and java file operations in a Java program
57.
Which class in the java.io package is used for reading data from a file in Java?
a)
FileReader
b)
FileWriter
c)
BufferedReader
d)
PrintWriter
58.
Which of the following methods is used to write data to a file using the FileWriter class in Java?
a)
write()
b)
append()
c)
output()
d)
add()
59.
What does the File class in the java.io package represent in Java?
a)
A virtual representation of a file system
b)
A file and directory path
c)
A file object for storing binary data
d)
A class for handling streams of bytes
60.
Which of the following classes is used to create an object that can be used to read characters from a file in Java?
a)
FileReader
b)
InputStreamReader
c)
BufferedReader
d)
Both A and C
61.
Which of the following statements creates a File object for the file named "data.txt"?
a)
File data = new File("data.txt");
b)
File data = new File("data.txt");
c)
File data = "data.txt";
d)
File data = new File();
62.
How can you handle exceptions when working with file I/O operations in Java?
a)
By using a try-catch block to catch IOException
b)
By using the throw keyword inside the method
c)
By using finally to close the file
d)
Both A and C
63.
How would you read a file line by line using a BufferedReader in Java?
a)
By using the readLine() method in a loop until it returns null
b)
By using the nextLine() method in a loop
c)
By using read() and converting the data to a string
d)
By using FileReader.read() in a loop
64.
When writing data to a file using PrintWriter, how can you automatically create a new file if it does not exist?
a)
By using the new File() constructor
b)
By checking if the file exists and using createNewFile()
c)
By using PrintWriter constructor with an existing file path
d)
By calling write() first to create the file
65.
Which of the following methods is used to append data to an existing file in Java?
a)
FileWriter("file.txt", true)
b)
BufferedWriter("file.txt")
c)
FileWriter("file.txt", false)
d)
FileReader("file.txt")
66.
How can you check if a file exists in Java using the File class?
a)
file.exists()
b)
file.isExist()
c)
file.available()
d)
file.getName()
67.
Which method from the File class is used to create a new directory in Java?
a)
mkdir()
b)
createDir()
c)
createNewFile()
d)
newDirectory()
68.
If you want to read a file character by character using FileReader, which method would you use?
a)
read()
b)
readLine()
c)
next()
d)
getChar()
69.
In Java, what is the correct way to write a file and close it properly after writing?
a)
FileWriter.write(); FileWriter.close();
b)
BufferedWriter.write(); File.close();
c)
PrintWriter.write(); PrintWriter.close();
d)
FileWriter.write(); FileWriter.flush();
70.
If you want to read from a file and store the data into an array, which method should you use?
a)
readAll()
b)
read() with FileReader and store it in an array
c)
getBytes() to read file data into a byte array
d)
FileReader in conjunction with BufferedReader.read()
Competency 6: Apply reading and writing classes in writing a Java program
71.
Which class is used for reading from a file in Java?
a)
FileReader
b)
FileWriter
c)
BufferedWriter
d)
PrintWriter
72.
What is the primary purpose of the BufferedReader class in Java?
a)
To write data to a file
b)
To read data from a file more efficiently
c)
To handle input from the console
d)
To delete data from a file
73.
Which of the following methods is used to read a single character from a file using a FileReader object?
a)
read()
b)
readLine()
c)
nextLine()
d)
getChar()
74.
How do you write data to a file using the PrintWriter class in Java?
a)
By using the println() method
b)
By using the write() method
c)
By using the append() method
d)
Both A and B
75.
In Java, which method is used to flush the output stream and ensure data is written to the file?
a)
flush()
b)
write()
c)
close()
d)
clear()
76.
What is the function of the close() method in file handling in Java?
a)
To start writing data to a file
b)
To ensure all data is written and release system resources
c)
To open a file for writing
d)
To delete the contents of the file
77.
Which of the following is the correct way to read data line by line from a file using BufferedReader?
a)
readLine()
b)
read()
c)
nextLine()
d)
getLine()
78.
What class would you use to write formatted output to a file in Java?
a)
FileWriter
b)
PrintWriter
c)
BufferedWriter
d)
OutputStreamWriter
79.
Which of the following statements correctly closes a file after writing data in Java?
a)
file.close();
b)
close(file);
c)
fileWriter.close();
d)
file.closeFile();
80.
How would you read the contents of a file and store each line in an array using Java?
a)
By using BufferedReader.read() and split()
b)
By using FileReader.read() and array[]
c)
By using BufferedReader.readLine() in a loop and storing it in an array
d)
By using FileWriter.readLine() to store lines in an array
81.
How can you handle an IOException when reading or writing a file in Java?
a)
By using a try-catch block to catch IOException
b)
By using throws IOException in the method signature
c)
By using finally to handle the exception after the program ends
d)
Both A and B
82.
Which method in PrintWriter would you use to write a string to a file?
a)
write()
b)
println()
c)
print()
d)
Both A and B
83.
If you want to read data from a file and write it to another file, which approach would you use in Java?
a)
Use FileReader to read and FileWriter to write
b)
Use BufferedReader to read and BufferedWriter to write
c)
Use FileInputStream to read and FileOutputStream to write
d)
All of the above
84.
How would you ensure that data is written to a file using PrintWriter without overwriting existing content?
a)
By using FileWriter with the true argument to append
b)
By using PrintWriter.append()
c)
By using BufferedWriter.write()
d)
By calling flush() after each write
85.
Which of the following code snippets correctly writes data to a file using BufferedWriter?
a)
BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
bw.write("Hello, World!");
bw.close();
b)
BufferedWriter bw = new BufferedWriter(new PrintWriter("output.txt"));
bw.write("Hello, World!");
bw.close();
c)
BufferedWriter bw = new BufferedWriter("output.txt");
bw.append("Hello, World!");
bw.close();
d)
BufferedWriter bw = new BufferedWriter(new FileReader("output.txt"));
bw.write("Hello, World!");
bw.close();
86.
How do you read all contents from a file into a string using BufferedReader?
a)
Use readLine() to read each line and append it to a string
b)
Use read() method to read all characters at once
c)
Use BufferedReader.read() and process each byte
d)
Use BufferedReader.readAll() to fetch all data
87.
If you're handling large files in Java, which class should you use to efficiently read data without consuming too much memory?
a)
FileReader
b)
BufferedReader
c)
PrintWriter
d)
FileInputStream
88.
What is the result of calling flush() on a PrintWriter object in Java?
a)
It clears the content of the file
b)
It writes all buffered data to the file
c)
It closes the file
d)
It resets the PrintWriter to its initial state
89.
If you want to open a file for both reading and writing, which class should you use in Java?
a)
RandomAccessFile
b)
FileReader
c)
FileWriter
d)
BufferedReader
90.
How can you handle multiple exceptions while performing file I/O operations in Java?
a)
By using multiple catch blocks for different exceptions
b)
By using a general catch (Exception e) block
c)
By using a try-catch-finally block
d)
Both A and C
91.
What happens if you try to write to a file that is currently open for reading in Java?
a)
It throws an IOException
b)
It will succeed without any issues
c)
The file content will be deleted
d)
The program will enter into an infinite loop
92.
When using FileWriter, how can you specify that the file should be overwritten?
a)
By passing false as the second argument to FileWriter constructor
b)
By passing true as the second argument to FileWriter constructor
c)
By using flush() before writing
d)
By using the overwrite() method
93.
How can you ensure proper closing of resources like files in Java after an exception occurs?
a)
By using a finally block
b)
By manually closing the file after every operation
c)
By using close() inside catch block
d)
By calling flush() in catch block
94.
What is the advantage of using BufferedReader over FileReader for reading files in Java?
a)
It reads the file character by character
b)
It reads the file more efficiently by buffering data
c)
It allows for writing data to the file
d)
It makes reading binary files easier
95.
When writing to a file, what should you do to ensure that all content is written out before closing the file?
a)
Use flush()
b)
Use close()
c)
Use write()
d)
Use clear()
96.
How can you handle encoding issues when reading a file in Java?
a)
By using InputStreamReader with a specified encoding
b)
By using BufferedReader with write()
c)
By using OutputStream
d)
By using FileInputStream
97.
If you want to create a file and check if it already exists before creating it, what method would you use?
a)
createNewFile()
b)
mkdir()
c)
exists()
d)
createFile()
98.
When reading a file with BufferedReader, what should you do to avoid errors when the file is empty?
a)
Check for null when calling readLine()
b)
Use a catch block for EOFException
c)
Use FileReader for reading binary data
d)
Leave out any exception handling
99.
How would you ensure that a file is properly created if it doesn't exist when using FileWriter in Java?
a)
Use createNewFile() method before writing
b)
Write data and the file will automatically be created
c)
Use FileWriter constructor with true argument
d)
Use flush() to create the file
100.
What is the best way to handle large data files when reading and writing in Java?
a)
Use streams to read and write data efficiently
b)
Use a single read() method to read the entire file
c)
Use BufferedWriter to handle large amounts of data
d)
Both A and C
100 %
