Font size
WorksheetsProgramming Concepts: Procedural vs Object-Oriented Programming
Total questions: 50
Worksheet time: 1hrs 15mins
Which of the following is a major limitation of procedural programming?
It promotes data security
It allows data and functions to be linked together
It focuses on functions, not data, leading to poor data security
It supports code reusability through inheritance
Why is Object-Oriented Programming (OOP) needed?
To make programs longer and more complex
To avoid using functions
To structure programs around data and objects for better reusability and security
To reduce the number of variables used
In Object-Oriented Programming, an object is best described as:
A collection of unrelated variables
A real-world entity containing data and behavior
Only a function
A class definition
What is data encapsulation?
Hiding implementation details and combining data with methods in a single unit
Allowing free access to data
Dividing data and functions into separate blocks
Using inheritance to access private data
Data abstraction refers to:
Displaying all the details of an object
Showing only essential features while hiding internal details
Writing long programs
Overloading operators
Which feature of OOP allows an object to take on many forms?
Encapsulation
Polymorphism
Abstraction
Inheritance
What is inheritance in OOP?
A. A mechanism where one class acquires properties of another class
B. A process of hiding data from other classes
C. A method for overloading functions
D. A process to convert data types
The process of linking a function call to its definition at runtime is known as:
Static binding
Dynamic binding
Encapsulation
Overloading
In OOP, message passing refers to:
Sending data directly to variables
Calling methods of an object using object references
Sharing memory between objects
Transferring code between files
Which access modifier allows a class member to be accessed only within the same class?
Public
Protected
Private
Default
Which of the following correctly defines a method in a class?
A variable declared inside a class
A block of code that performs a specific task and belongs to a class
A comment inside the program
A data type used in a class
What is the purpose of the this keyword in Java?
To refer to the parent class
To refer to the current object of the class
To refer to the child class
To refer to a static method
3. Method overloading means:
defining multiple methods with the same name but different parameters
defining multiple methods with the same name and same parameters
defining methods with different names but same parameters
defining methods with different names and different parameters
Which keyword is used to allocate memory for an object in Java?
malloc
alloc
new
create
What are static data members?
Variables that are local to a function
Variables that belong to each object separately
Variables shared by all objects of the class
Variables that cannot be initialized
What is the main purpose of a constructor?
To destroy objects
To initialize objects when they are created
To overload methods
To store constant values
Which of the following is not true about constructors?
A. A constructor has the same name as the class
B. A constructor has no return type
What is constructor overloading?
Having multiple constructors in a class with different parameter lists
Having multiple destructors in a class
Using inheritance to create constructors
Declaring static constructors
What is garbage collection in Java?
Process of manually freeing memory using delete keyword
Process of automatic memory management that removes unused objects
Process of sorting data
Process of creating arrays
What is a destructor or finalizer used for?
To initialize data members
To allocate memory
To perform cleanup operations before an object is destroyed
To overload constructors
What is inheritance in Object-Oriented Programming?
The process of creating multiple objects from one class
The process where one class acquires the properties and behaviors of another class
The process of hiding data
The process of linking functions dynamically
What is inheritance in object-oriented programming?
The process of hiding data from users
The process where one class acquires the properties and behaviors of another class
The process of creating objects
The process of restricting the use of objects
Which of the following best describes the need for inheritance?
To hide data from users
To improve code reusability and avoid redundancy
To reduce the number of classes in a program
To restrict the use of objects
Which type of inheritance allows a class to inherit from more than one base class?
Single inheritance
Multiple inheritance
Multilevel inheritance
Hybrid inheritance
What is the main benefit of using inheritance?
Increases data redundancy
Simplifies maintenance and promotes code reusability
Makes debugging more difficult
Prevents method overriding
What is method overriding?
Writing two methods with the same name but different parameters in the same class
Redefining a method of a parent class in the derived class with the same signature
Overloading constructors
Writing a static method inside a class
What happens when a derived class object is created in Java regarding constructors?
Only the derived class constructor is called
Only the base class constructor is called
Base class constructor is called first, then derived class constructor
Constructors are not called automatically
Which of the following statements about abstract classes is true?
Abstract classes can be instantiated directly
Abstract classes can contain abstract and non-abstract methods
Abstract classes cannot have constructors
Abstract methods must have a body
What is an interface primarily used for?
To store constants
To provide a blueprint for classes by defining method signatures without implementation
To hide data members
To create objects directly
Which of the following is an example of compile-time polymorphism?
Method overriding
Method overloading
Dynamic binding
Interface implementation
How does polymorphism improve software reuse?
By allowing objects of different classes to be treated as objects of a common superclass
By restricting inheritance
By avoiding the use of abstract classes
By increasing the number of classes needed
What will be the output of the following code? try { int a = 10 / 0; System.out.println("Inside try block"); } catch (ArithmeticException e) { System.out.println("Exception caught"); } System.out.println("Program continues...");
Inside try block
Exception caught
Program continues...
Both B and C
What will happen if no catch block matches the thrown exception?
Program runs successfully
The JVM terminates the program with an error message
The exception is ignored
The exception is converted into a warning
try { int x = 5 / 0; } catch (ArithmeticException e) { System.out.println("Inner catch"); } catch (Exception e) { System.out.println("Outer catch"); }
Inner catch
Outer catch
Both Inner and Outer catch
No output
What will be the output?
Index error
General error
Both
No output
What is the output of this code using Generics?
[10, 20, Hello]
Compilation error at Line X
Runtime error
No output
Which of the following correctly defines a generic method?
public void display(T data) { }
public
public void
What is the output of this program?
List
Alice
Bob
Charlie
null
What happens if you try to remove a non-existent element from a set in Java?
Throws Exception
Prints [A]
Prints []
Compilation error
What will the following code print?
LinkedList
[2, 5, 10]
[5, 10, 2]
[10, 5, 2]
[2, 10, 5]
A. [5, 10, 2] B. [2, 5, 10] C. [10, 5, 2] D. [2, 10, 5]
A. [5, 10, 2]
B. [2, 5, 10]
C. [10, 5, 2]
D. [2, 10, 5]
What is the main purpose of streams in Java file handling?
To create GUI applications
To transfer data between source and destination
To store data permanently
To execute SQL queries
Which of the following classes is used for byte stream input in Java?
FileReader
FileWriter
FileInputStream
BufferedReader
Which stream class is used for reading characters from a file?
FileInputStream
FileReader
ObjectInputStream
DataInputStream
The File class in Java is used to:
Read data from a file
Write data to a file
Which of the following is true about Random Access File?
It allows only sequential access
It allows both read and write operations at any position in the file
It can read only text files
It is used for networking
Which exception is thrown during input/output operations if an error occurs?
IOException
ArithmeticException
NullPointerException
FileException
Which class allows buffered reading and writing for better performance?
DataInputStream
BufferedReader and BufferedWriter
ObjectOutputStream
PrintStream
The Singleton design pattern ensures that:
Multiple instances of a class can be created
Only one instance of a class exists throughout the application
No instances of a class can be created
The class can be inherited
What is the purpose of the Adapter design pattern?
To add new functionality dynamically
To allow incompatible interfaces to work together
To manage object creation
To provide global access to data
