OOP With Java Workbook
OOP With Java Workbook
&
Department of Artificial Intelligence and Machine Learning
By
Ms. JEEVITHA
HOW TO USE THE WORKBOOK
MODULE TITLE
5 Multithreaded Programming
Enumerations, Type Wrappers and
Autoboxing
REFERENCES
• http://gate-exam.in
• https://www.geeksforgeeks.org
• https://www.javatpoint.com/
• https://www.baeldung.com/java-blogs
MODULE-1
An Overview of Java,Data Types, Variables, and Arrays,Operators and
Control Statements.
#### Object-Oriented Programming (Two Paradigms, Abstraction, The Three OOP
Principles)
1. **What are the two main programming paradigms often associated with Object-Oriented
Programming (OOP)?**
d) To declare variables
#### Lexical Issues (Whitespace, Identifiers, Literals, Comments, Separators, The Java
Keywords)
b) function
c) variable
d) module
- Answer: a) class
a) To execute code
a) Semicolon (;)
b) Comma (,)
c) Period (.)
d) Colon (:)
a) double
b) char
c) int
d) boolean
- Answer: c) int
d) Creating an array
b) Automatically upgrading a variable to a larger data type to avoid data loss in calculations
- Answer: b) Automatically upgrading a variable to a larger data type to avoid data loss in
calculations
c) Creating a single variable to store multiple values of the same data type
- Answer: c) Creating a single variable to store multiple values of the same data type
5. **What does type inference with local variables in Java allow you to do?**
a) Relational operators
c) Assignment operators
d) Arithmetic operators
a) +
b) &&
c) ||
d) ==
- Answer: b) &&
7. **In Java, how can you change the order of evaluation in an expression?**
a) By using parentheses ()
a) +
b) &&
c) ||
d) ==
- Answer: c) ||
d) To declare a variable
a) Iteration statements
b) Selection statements
c) Jump statements
2. **In Java, which selection statement allows you to execute a block of code only if a
condition is true?**
a) if statement
b) switch statement
c) while loop
d) for loop
- Answer: a) if statement
4. **In Java, which iteration statement is used for creating a loop that repeats a block of code
as long as a condition is true?**
a) while loop
b) do-while loop
c) for loop
d) for-each loop
c) while (condition)
7. **What is the primary benefit of using local variable type inference in a for loop in
Java?**
8. **In Java, which jump statement is used to exit a loop prematurely and transfer control to
the end of the loop?**
a) break
b) continue
c) return
d) exit
- Answer: a) break
9. **Which control statement in Java is used to skip the remaining code in the current
iteration of a loop and move to the next iteration?**
a) break
b) continue
c) return
d) exit
- Answer: b) continue
MODULE-2
a) To declare methods
b) To create objects
c) To assign variables
d) To define constructors
b) Constructor
d) Class declaration
- Answer: b) Constructor
a) To declare a class
b) To store a method
d) To create constructors
a) Object instances
b) Variable declarations
d) Constructors
b) The superclass
a) Object
b) Method
c) Constructor
d) Variable
- Answer: a) Object
d) Declaring variables
b) Implementing loops
c) Dividing a program into smaller, more manageable parts
c) Creating constructors
- Answer: a) A method that can be accessed without creating an instance of the class
a) To define a method that can be accessed without creating an instance of the class
9. **What are nested and inner classes in Java mainly used for?**
d) To create constructors
MODULE-3
Inheritance and Interface
#### Inheritance: Inheritance Basics, Using super, Multilevel Hierarchy, Constructors,
Method Overriding, Dynamic Method Dispatch, Abstract Classes, final in Inheritance, Local
Variable Type Inference, The Object Class
a) To create objects
c) To define methods
d) To establish a relationship between classes where one class inherits properties and
behaviors from another.
- Answer: d) To establish a relationship between classes where one class inherits properties
and behaviors from another.
2. **In Java, what does the "super" keyword primarily refer to in the context of
inheritance?**
a) Creating new methods in the subclass with the same name as in the superclass
- Answer: a) Creating new methods in the subclass with the same name as in the superclass.
b) To determine the class of an object at runtime and call the appropriate method
- Answer: b) To determine the class of an object at runtime and call the appropriate method.
7. **In Java, what is the main purpose of abstract classes?**
- Answer: d) To serve as a blueprint for other classes and enforce method declarations.
8. **What does the "final" keyword in Java signify when used with inheritance?**
9. **In Java, what is the primary purpose of using local variable type inference (var) in the
context of inheritance?**
a) To create subclasses
b) To avoid inheritance
10. **In Java, what is the "Object" class primarily used for?**
a) To create objects
b) To define constructors
#### Interfaces: Interfaces, Default Interface Methods, Static Methods in an Interface, Private
Interface Methods
a) To create objects
b) To define constructors
d) To declare variables
d) To declare variables
c) To provide a common utility method that can be called on the interface itself
d) To declare variables
- Answer: c) To provide a common utility method that can be called on the interface itself.
4. **In Java, what is the significance of private interface methods?**
c) They are used for internal utility methods within the interface.
- Answer: c) They are used for internal utility methods within the interface.
MODULE-4
Packages and Exception
b) To declare methods
c) To create objects
d) To define constructors
a) Members within a package can only be accessed within the same class.
b) Members within a package can be accessed from any class within the same package.
c) Members within a package can only be accessed from classes in different packages.
- Answer: b) Members within a package can be accessed from any class within the same
package.
4. **In Java, what is the purpose of the "import" statement when working with packages?**
- Answer: d) Package names should be in lowercase and follow a reverse domain structure.
Certainly, here are multiple-choice questions on the topic of Exceptions in Java, along with
the correct answers:
a) NullPointerException
b) ArrayIndexOutOfBoundsException
c) FileNotFoundException
d) ArithmeticException
- Answer: c) FileNotFoundException
4. **What is the purpose of the "try" and "catch" blocks in exception handling?**
d) To declare variables
5. **In Java, what is the primary purpose of the "finally" block in exception handling?**
d) To rethrow exceptions
b) To catch exceptions
c) To declare variables
8. **Which type of exception represents a logical error in the program, such as dividing by
zero?**
a) Checked exception
b) Unchecked exception
c) Runtime exception
d) Custom exception
c) To rethrow exceptions
d) To suppress exceptions
- Answer: b) The first thread that starts when a Java program is executed.
5. **How can you make one thread wait for another thread to finish its execution in Java?**
b) To stop a thread
c) To ensure that only one thread can access a shared resource at a time
- Answer: c) To ensure that only one thread can access a shared resource at a time.
a) `stop()`
b) `suspend()`
c) `resume()`
d) `halt()`
- Answer: a) `stop()`.
11. **What are the possible states that a Java thread can be in?**
**Enumerations (Enum):**
d) To define constructors
a) `toArray()`
b) `list()`
c) `values()`
d) `constants()`
- Answer: c) `values()`.
**Type Wrappers:**
4. **In Java, what is the purpose of a type wrapper class, such as `Integer` or `Double`?**
b) To define constructors
d) To manage arrays
a) `Bool`
b) `Boolean`
c) `bool`
d) `Bit`
- Answer: b) `Boolean`.
b) To define constructors
d) To encapsulate integers
a) `Int`
b) `Integer`
c) `int`
d) `Number`
- Answer: b) `Integer`.
d) Defining constructors
- Answer: b) Converting primitive types to wrapper objects and vice versa automatically.
c) Automatically when assigning primitive values to wrapper objects and vice versa
d) Only in loops
- Answer: c) Automatically when assigning primitive values to wrapper objects and vice
versa.
10. **Which wrapper classes are used for autoboxing and unboxing Boolean and Character
values in Java?**