0% found this document useful (0 votes)
8 views

OOP With Java Workbook

The document is a workbook for the Object-Oriented Programming with Java course, effective from the academic year 2023-2024, targeting III-A & B sections. It includes modules covering Java fundamentals, classes, inheritance, and interfaces, along with practice questions and references for further study. The workbook aims to facilitate day-to-day topic revision, self-assessment, and GATE preparation.

Uploaded by

shreya.y2612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

OOP With Java Workbook

The document is a workbook for the Object-Oriented Programming with Java course, effective from the academic year 2023-2024, targeting III-A & B sections. It includes modules covering Java fundamentals, classes, inheritance, and interfaces, along with practice questions and references for further study. The workbook aims to facilitate day-to-day topic revision, self-assessment, and GATE preparation.

Uploaded by

shreya.y2612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Department of Computer Science and Engineering(AI&ML)

&
Department of Artificial Intelligence and Machine Learning

OBJECT ORIENTED PROGRAMMING WITH JAVA


WORKBOOK
(Effective from the academic year 2023 -2024)

Semesters: III-A & B section


Subject Code: BCS306A

By
Ms. JEEVITHA
HOW TO USE THE WORKBOOK

• Day-to-day topic revision


• Self-assessment
• Practice problems
• GATE preparation
• CONTENTS

MODULE TITLE

1 An Overview of Java,Data Types,


Variables, and Arrays,Operators and
Control Statements.
2 Introducing Classes,Methods and Classes

3 Inheritance and Interfacs

4 Packages and Exceptions

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)?**

a) Procedural and Functional

b) Imperative and Declarative

c) Inheritance and Polymorphism

d) Abstraction and Encapsulation

- Answer: d) Abstraction and Encapsulation

2. **In OOP, what is the primary goal of abstraction?**

a) Exposing all internal details of an object

b) Hiding the complexity of an object's internal workings

c) Implementing all features of an object

d) Making all object members public

- Answer: b) Hiding the complexity of an object's internal workings


3. **Which of the following represents the three core principles of OOP?**

a) Abstraction, Overloading, Polymorphism

b) Inheritance, Encapsulation, Polymorphism

c) Procedural, Functional, Declarative

d) While, For, Do-While

- Answer: b) Inheritance, Encapsulation, Polymorphism

#### Using Blocks of Code

4. **In Java, what is the purpose of using code blocks?**

a) To create new data types

b) To define classes and objects

c) To group statements and control scope

d) To declare variables

- Answer: c) To group statements and control scope

#### Lexical Issues (Whitespace, Identifiers, Literals, Comments, Separators, The Java
Keywords)

5. **What is the primary function of whitespace in Java code?**

a) To separate words within identifiers

b) To make the code more difficult to read

c) To indicate the end of a statement

d) To improve code indentation and readability

- Answer: d) To improve code indentation and readability

6. **Which of the following is considered a Java keyword?**


a) class

b) function

c) variable

d) module

- Answer: a) class

7. **What do Java literals represent in code?**

a) Variables that store values

b) Reserved words with special meanings

c) Constants with fixed values

d) Names for classes and objects

- Answer: c) Constants with fixed values

8. **What is the primary role of comments in Java code?**

a) To execute code

b) To make code run faster

c) To improve code readability and provide explanations

d) To generate compile-time errors

- Answer: c) To improve code readability and provide explanations

9. **Which of the following is not a separator in Java code?**

a) Semicolon (;)

b) Comma (,)

c) Period (.)

d) Colon (:)

- Answer: d) Colon (:)


Certainly, here are multiple-choice questions for Module 1 on Data Types, Variables, and
Arrays, along with the correct answers:

#### Data Types, Variables, and Arrays

1. **Which data type is used to store whole numbers in Java?**

a) double

b) char

c) int

d) boolean

- Answer: c) int

2. **What is the purpose of type conversion in Java?**

a) Converting a variable to a different data type explicitly

b) Assigning a value to a variable

c) Declaring a new variable

d) Creating an array

- Answer: a) Converting a variable to a different data type explicitly

3. **In Java, what is automatic type promotion in expressions?**

a) Converting a variable to a different data type explicitly

b) Automatically upgrading a variable to a larger data type to avoid data loss in calculations

c) Assigning a value to a variable without specifying its data type

d) Changing a variable's name without changing its data type

- Answer: b) Automatically upgrading a variable to a larger data type to avoid data loss in
calculations

4. **What is an array in Java used for?**


a) Storing multiple values of different data types

b) Defining new data types

c) Creating a single variable to store multiple values of the same data type

d) Declaring constants with fixed values

- 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) Automatically convert variables to different data types

b) Declare variables without specifying their data types explicitly

c) Declare arrays more efficiently

d) Use variables without initialization

- Answer: b) Declare variables without specifying their data types explicitly

#### Operators: Arithmetic, Relational, Boolean Logical, Assignment, ?, Precedence,


Parentheses

1. **Which type of operator is used to perform mathematical calculations in Java?**

a) Relational operators

b) Boolean logical operators

c) Assignment operators

d) Arithmetic operators

- Answer: d) Arithmetic operators

2. **What is the primary purpose of relational operators in Java?**

a) To perform arithmetic calculations

b) To combine two Boolean values

c) To compare values and determine relationships


d) To assign values to variables

- Answer: c) To compare values and determine relationships

3. **Which operator is used for logical AND in Java?**

a) +

b) &&

c) ||

d) ==

- Answer: b) &&

4. **What does the assignment operator (=) do in Java?**

a) Compares two values for equality

b) Adds two numbers together

c) Assigns a value to a variable

d) Performs a logical NOT operation

- Answer: c) Assigns a value to a variable

5. **In Java, what is the purpose of the "?" operator?**

a) It is not a valid operator in Java

b) It is used for conditional (ternary) expressions

c) It is used for bitwise operations

d) It is used for string concatenation

- Answer: b) It is used for conditional (ternary) expressions

6. **What does operator precedence in Java refer to?**

a) The order in which operators are evaluated in an expression

b) The order in which variables are declared


c) The order of execution for control statements

d) The priority of different data types

- Answer: a) The order in which operators are evaluated in an expression

7. **In Java, how can you change the order of evaluation in an expression?**

a) By using parentheses ()

b) By using square brackets []

c) By using curly braces {}

d) By using angle brackets <>

- Answer: a) By using parentheses ()

8. **Which operator is used for logical OR in Java?**

a) +

b) &&

c) ||

d) ==

- Answer: c) ||

9. **What is the primary purpose of the "==" operator in Java?**

a) To assign a value to a variable

b) To perform arithmetic calculations

c) To compare two values for equality

d) To declare a variable

- Answer: c) To compare two values for equality

#### Control Statements: Selection Statements, Iteration Statements, Jump Statements


1. **Which type of control statement is used for making decisions based on conditions in
Java?**

a) Iteration statements

b) Selection statements

c) Jump statements

d) All of the above

- Answer: b) Selection 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

3. **What is the purpose of the "switch" statement in Java?**

a) To create a loop that repeats a block of code

b) To define a condition and execute code based on that condition

c) To define a control structure for multiple conditional branches

d) To exit a loop prematurely

- Answer: c) To define a control structure for multiple conditional branches

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

- Answer: a) while loop

5. **What is the primary purpose of the "for" loop in Java?**

a) To create a loop with a fixed number of iterations

b) To define a single conditional branch

c) To exit a loop prematurely

d) To execute a block of code only if a condition is true

- Answer: a) To create a loop with a fixed number of iterations

6. **Which of the following represents a "for-each" loop in Java?**

a) for (int i = 0; i < 10; i++)

b) for (String item : items)

c) while (condition)

d) do { /* code */ } while (condition)

- Answer: b) for (String item : items)

7. **What is the primary benefit of using local variable type inference in a for loop in
Java?**

a) It allows you to define global variables.

b) It reduces code readability.

c) It simplifies variable declaration.

d) It speeds up loop execution.

- Answer: c) It simplifies variable declaration.

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

Introducing Classes,Methods and Classes


#### Introducing Classes: Class Fundamentals, Objects, Methods, Constructors, this
Keyword, Garbage Collection

1. **What is the primary purpose of a class in Java?**

a) To declare methods

b) To create objects

c) To assign variables

d) To define constructors

- Answer: b) To create objects

2. **Which of the following is used to create an object from a class in Java?**


a) Method

b) Constructor

c) Object reference variable

d) Class declaration

- Answer: b) Constructor

3. **In Java, what is an object reference variable used for?**

a) To declare a class

b) To store a method

c) To refer to an object of a class

d) To create constructors

- Answer: c) To refer to an object of a class

4. **What do methods in a class represent in Java?**

a) Object instances

b) Variable declarations

c) Actions or behaviors that objects can perform

d) Constructors

- Answer: c) Actions or behaviors that objects can perform

5. **What is the primary purpose of a constructor in Java?**

a) To create object reference variables

b) To declare methods in a class

c) To initialize object properties and perform setup

d) To define object behaviors

- Answer: c) To initialize object properties and perform setup


6. **In Java, what does the "this" keyword refer to?**

a) The current class

b) The superclass

c) The class declaration

d) The current object

- Answer: d) The current object

7. **What is garbage collection in Java?**

a) A process for manually deleting objects

b) A way to declare variables

c) The automatic removal of unreferenced objects to free up memory

d) A method for creating objects

- Answer: c) The automatic removal of unreferenced objects to free up memory

8. **Which of the following is not a fundamental part of a class in Java?**

a) Object

b) Method

c) Constructor

d) Variable

- Answer: a) Object

#### Methods and Classes: Overloading, Objects as Parameters, Argument Passing,


Returning Objects, Recursion, Access Control, static, final, Nested and Inner Classes

1. **In Java, what is method overloading primarily about?**

a) Creating new methods with the same name

b) Redefining existing methods

c) Assigning multiple methods to a variable


d) Defining constructors

- Answer: a) Creating new methods with the same name

2. **What does it mean to pass an object as a parameter to a method in Java?**

a) It involves creating a new object inside the method.

b) It refers to changing the value of the object.

c) It allows you to send an object to a method to be operated on.

d) It creates a new object reference variable.

- Answer: c) It allows you to send an object to a method to be operated on.

3. **In Java, what is argument passing primarily concerned with?**

a) Returning values from a method

b) Creating new objects

c) Passing data to methods

d) Declaring variables

- Answer: c) Passing data to methods

4. **What is the main purpose of returning objects from methods in Java?**

a) To create new objects

b) To assign values to variables

c) To pass objects as parameters

d) To encapsulate and return processed data

- Answer: d) To encapsulate and return processed data

5. **What is recursion in Java primarily used for?**

a) Defining methods with the same name

b) Implementing loops
c) Dividing a program into smaller, more manageable parts

d) A method calling itself

- Answer: d) A method calling itself

6. **In Java, what does access control refer to?**

a) Defining methods with the same name

b) Determining which methods can be accessed by other classes

c) Creating constructors

d) Controlling object references

- Answer: b) Determining which methods can be accessed by other classes

7. **In Java, what does the "static" keyword primarily indicate?**

a) A method that can be accessed without creating an instance of the class

b) A method that is dynamic and changes over time

c) A method that is recursive

d) A method that is final and cannot be overridden

- Answer: a) A method that can be accessed without creating an instance of the class

8. **What is the primary purpose of the "final" keyword in Java?**

a) To define a method that can be accessed without creating an instance of the class

b) To specify that a class cannot be extended or a method cannot be overridden

c) To create inner classes

d) To indicate that a method is recursive

- Answer: b) To specify that a class cannot be extended or a method cannot be overridden

9. **What are nested and inner classes in Java mainly used for?**

a) To create standalone classes


b) To define methods with the same name

c) To group one class inside another class

d) To create constructors

- Answer: c) To group one class inside another class

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

1. **What is the main purpose of inheritance in Java?**

a) To create objects

b) To group related classes into packages

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) It specifies a class as final and cannot be extended.

b) It calls the superclass constructor.

c) It is used to declare variables.

d) It defines the main method.

- Answer: b) It calls the superclass constructor.


3. **What is a multilevel hierarchy in Java primarily about?**

a) Creating unrelated classes

b) Establishing a single level of inheritance

c) Defining methods with the same name

d) Creating a chain of inheritance with multiple levels of classes

- Answer: d) Creating a chain of inheritance with multiple levels of classes.

4. **In Java, when are constructors executed in an inheritance hierarchy?**

a) Constructors are executed only in the subclass.

b) Constructors are executed only in the superclass.

c) Constructors are executed in both the superclass and subclass.

d) Constructors are not executed in inheritance.

- Answer: c) Constructors are executed in both the superclass and subclass.

5. **What is method overriding primarily about in Java?**

a) Creating new methods in the subclass with the same name as in the superclass

b) Defining multiple constructors in a class

c) Declaring final methods

d) Implementing static methods

- Answer: a) Creating new methods in the subclass with the same name as in the superclass.

6. **What is dynamic method dispatch in Java used for?**

a) To select a constructor at runtime

b) To determine the class of an object at runtime and call the appropriate method

c) To create abstract classes

d) To prevent method overriding

- 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?**

a) To define concrete methods

b) To create unrelated classes

c) To group unrelated classes

d) To serve as a blueprint for other classes and enforce method declarations

- 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?**

a) It indicates that a class cannot be extended.

b) It is used to define constructors.

c) It denotes that a method can be overridden.

d) It specifies that a class is abstract.

- Answer: a) It indicates that a class cannot be extended.

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

c) To simplify variable declarations and improve code readability

d) To create unrelated classes

- Answer: c) To simplify variable declarations and improve code readability.

10. **In Java, what is the "Object" class primarily used for?**

a) To create objects

b) To define constructors

c) To serve as the superclass of all classes


d) To prevent method overriding

- Answer: c) To serve as the superclass of all classes.

#### Interfaces: Interfaces, Default Interface Methods, Static Methods in an Interface, Private
Interface Methods

1. **What is the primary purpose of an interface in Java?**

a) To create objects

b) To define constructors

c) To establish a contract for classes that implement it

d) To declare variables

- Answer: c) To establish a contract for classes that implement it.

2. **In Java, what are default interface methods used for?**

a) To define methods that must be overridden by implementing classes

b) To create unrelated classes

c) To provide a default implementation for methods in an interface

d) To declare variables

- Answer: c) To provide a default implementation for methods in an interface.

3. **What is the primary purpose of static methods in an interface in Java?**

a) To define methods that must be overridden by implementing classes

b) To create unrelated classes

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?**

a) They are used for declaring abstract methods in an interface.

b) They allow access to interface methods from outside classes.

c) They are used for internal utility methods within the interface.

d) They prevent implementing classes from using the interface methods.

- Answer: c) They are used for internal utility methods within the interface.

5. **Which of the following is true about interfaces in Java?**

a) An interface can have concrete methods with implementations.

b) An interface cannot declare any methods.

c) An interface can extend multiple other interfaces.

d) An interface can be instantiated as an object.

- Answer: c) An interface can extend multiple other interfaces.

MODULE-4
Packages and Exception

#### Packages: Packages, Packages and Member Access, Importing Packages

1. **In Java, what is the primary purpose of using packages?**

a) To group related classes and interfaces

b) To declare methods

c) To create objects

d) To define constructors

- Answer: a) To group related classes and interfaces

2. **Which of the following statements about packages is true?**


a) Packages are optional in Java, and you can choose not to use them.

b) Packages are only used for naming variables.

c) Packages are used for controlling access to methods and variables.

d) Packages are used for structuring and organizing code.

- Answer: d) Packages are used for structuring and organizing code.

3. **What is the significance of member access within a package in Java?**

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.

d) Members within a package cannot be accessed.

- 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?**

a) It allows you to import packages from other programming languages.

b) It is used to include a package's classes and interfaces in your program.

c) It is used to declare variables.

d) It is a reserved keyword and cannot be used.

- Answer: b) It is used to include a package's classes and interfaces in your program.

5. **Which of the following is true about package names in Java?**

a) Package names should always start with a capital letter.

b) Package names should be unique across all Java applications.

c) Package names should be separated by hyphens.

d) Package names should be in lowercase and follow a reverse domain structure.

- 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:

#### Exceptions: Exception-Handling Fundamentals, Exception Types, Uncaught


Exceptions, try and catch, Multiple catch Clauses, Nested try Statements, throw, throws,
finally, Java’s Built-in Exceptions, Creating Your Own Exception Subclasses, Chained
Exceptions

1. **What is the primary purpose of exception handling in Java?**

a) To cause exceptions intentionally

b) To handle errors and exceptional situations gracefully

c) To create custom exceptions

d) To terminate the program when an error occurs

- Answer: b) To handle errors and exceptional situations gracefully

2. **Which of the following represents a checked exception in Java?**

a) NullPointerException

b) ArrayIndexOutOfBoundsException

c) FileNotFoundException

d) ArithmeticException

- Answer: c) FileNotFoundException

3. **What happens when an uncaught exception occurs in a Java program?**

a) The program continues to execute normally.

b) The program terminates with an error message.

c) The program skips the code that caused the exception.

d) The program automatically handles the exception.


- Answer: b) The program terminates with an error message.

4. **What is the purpose of the "try" and "catch" blocks in exception handling?**

a) To create custom exceptions

b) To terminate the program

c) To define and handle exceptional conditions

d) To declare variables

- Answer: c) To define and handle exceptional conditions

5. **In Java, what is the primary purpose of the "finally" block in exception handling?**

a) To define custom exceptions

b) To catch multiple exceptions

c) To guarantee a block of code is always executed, whether an exception occurs or not

d) To rethrow exceptions

- Answer: c) To guarantee a block of code is always executed, whether an exception occurs


or not

6. **What is the role of the "throw" keyword in Java?**

a) To create custom exceptions

b) To catch exceptions

c) To declare variables

d) To terminate the program

- Answer: a) To create custom exceptions

7. **What does the "throws" keyword in Java indicate?**

a) It indicates that a method may throw exceptions.

b) It indicates that a method has caught all possible exceptions.


c) It is a reserved keyword and cannot be used.

d) It is used for creating custom exceptions.

- Answer: a) It indicates that a method may throw exceptions.

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

- Answer: c) Runtime exception

9. **What is the purpose of creating custom exception subclasses in Java?**

a) To catch built-in exceptions

b) To simplify the code

c) To handle exceptions more gracefully

d) To define and throw application-specific exceptions

- Answer: d) To define and throw application-specific exceptions

10. **What are chained exceptions in Java primarily used for?**

a) To handle multiple exceptions simultaneously

b) To create complex exception hierarchies

c) To rethrow exceptions

d) To suppress exceptions

- Answer: b) To create complex exception hierarchies


MODULE-5
Packages and Exception

Multithreaded Programming Enumerations, Type Wrappers and Autoboxing.

1. **What is the primary purpose of multithreading in Java?**

a) To create separate applications

b) To run multiple Java applications simultaneously

c) To execute multiple tasks concurrently within a single Java application

d) To enhance the performance of a single-threaded application

- Answer: c) To execute multiple tasks concurrently within a single Java application.

2. **What is the main thread in Java?**

a) A thread that is explicitly created using the `Thread` class

b) The first thread that starts when a Java program is executed

c) A thread with the highest priority

d) A thread that is suspended

- Answer: b) The first thread that starts when a Java program is executed.

3. **How can you create a new thread in Java?**

a) By extending the `java.lang.Thread` class

b) By creating an instance of the `java.lang.Thread` class

c) By using the `java.lang.Thread` interface

d) By using the `java.util.concurrent.Thread` class

- Answer: a) By extending the `java.lang.Thread` class.

4. **What does the `isAlive()` method in Java determine about a thread?**

a) Whether the thread is currently running


b) Whether the thread is suspended

c) Whether the thread has finished executing

d) Whether the thread has been started

- Answer: a) Whether the thread is currently running.

5. **How can you make one thread wait for another thread to finish its execution in Java?**

a) By using the `stop()` method

b) By using the `suspend()` method

c) By using the `join()` method

d) By using the `sleep()` method

- Answer: c) By using the `join()` method.

6. **What is the purpose of thread priorities in Java?**

a) To determine the order in which threads are executed

b) To ensure that higher-priority threads always execute first

c) To limit the maximum number of threads in an application

d) To define the thread's name

- Answer: a) To determine the order in which threads are executed.

7. **What is synchronization in Java primarily used for in the context of multithreading?**

a) To increase the thread priority

b) To stop a thread

c) To ensure that only one thread can access a shared resource at a time

d) To create a new thread

- Answer: c) To ensure that only one thread can access a shared resource at a time.

8. **How can threads communicate with each other in Java?**


a) By directly modifying the memory of other threads

b) By using shared variables

c) By stopping and starting threads

d) By using the `yield()` method

- Answer: b) By using shared variables.

9. **What is the purpose of suspending a thread in Java?**

a) To increase the thread's priority

b) To release resources held by the thread

c) To make the thread sleep for a specific duration

d) To temporarily halt the thread's execution

- Answer: d) To temporarily halt the thread's execution.

10. **Which method is used to stop a thread in Java?**

a) `stop()`

b) `suspend()`

c) `resume()`

d) `halt()`

- Answer: a) `stop()`.

11. **What are the possible states that a Java thread can be in?**

a) Running, Stopped, Sleeping

b) Active, Inactive, Terminated

c) New, Runnable, Blocked, Waiting, Timed Waiting, Terminated

d) Ready, Executing, Blocked

- Answer: c) New, Runnable, Blocked, Waiting, Timed Waiting, Terminated.


#### Enumerations, Type Wrappers, and Autoboxing: Enumerations, values() and valueOf()
Methods, Type Wrappers (Character, Boolean, Numeric Type Wrappers), Autoboxing,
Unboxing, Boolean and Character Values

**Enumerations (Enum):**

1. **What is the primary purpose of enumerations (enum) in Java?**

a) To represent a list of predefined constants

b) To create custom exceptions

c) To group unrelated classes

d) To define constructors

- Answer: a) To represent a list of predefined constants.

2. **Which method is used to obtain an array of enum constants in Java?**

a) `toArray()`

b) `list()`

c) `values()`

d) `constants()`

- Answer: c) `values()`.

3. **What is the primary use of the `valueOf()` method in Java enums?**

a) To retrieve the ordinal value of an enum constant

b) To return the name of an enum constant

c) To convert a string into an enum constant

d) To compare two enum constants

- Answer: c) To convert a string into an enum constant.

**Type Wrappers:**
4. **In Java, what is the purpose of a type wrapper class, such as `Integer` or `Double`?**

a) To create custom exceptions

b) To define constructors

c) To encapsulate primitive data types as objects

d) To manage arrays

- Answer: c) To encapsulate primitive data types as objects.

5. **Which type wrapper class is used to represent Boolean values in Java?**

a) `Bool`

b) `Boolean`

c) `bool`

d) `Bit`

- Answer: b) `Boolean`.

6. **What is the purpose of the `Character` type wrapper class in Java?**

a) To represent a single character as an object

b) To define constructors

c) To create custom exceptions

d) To encapsulate integers

- Answer: a) To represent a single character as an object.

7. **Which type wrapper class is used to represent integers in Java?**

a) `Int`

b) `Integer`

c) `int`

d) `Number`
- Answer: b) `Integer`.

**Autoboxing and Unboxing:**

8. **What is autoboxing primarily about in Java?**

a) Automatically creating instances of wrapper classes

b) Converting primitive types to wrapper objects and vice versa automatically

c) Creating custom exceptions

d) Defining constructors

- Answer: b) Converting primitive types to wrapper objects and vice versa automatically.

9. **When does autoboxing and unboxing occur in Java expressions?**

a) Only in arithmetic operations

b) Only when using conditional statements

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?**

a) `Bool` and `Char`

b) `Boolean` and `Character`

c) `Bit` and `String`

d) `Number` and `CharSequence`

- Answer: b) `Boolean` and `Character`.

You might also like