DEC_2023_OLE-V20UDS201 (#263)
Total Marks: 70
Total Duration: 150 minutes
Instructions
PART - A Answer all 25 MCQs (25 X 1 = 25 Marks)
PART - B Answer all 5 Descriptive Questions (5 X 5 = 25 Marks)
PART - C Answer the Descriptive Question (1 X 20 = 20 Marks)
Basic Instructions
1. You can freely navigate between different questions forward and backward using
Next and Previous buttons.
2. Finish button will be enabled only towards the end of the exam.
Instructions for DESCRIPTIVE questions requiring SCAN & UPLOAD
1. Make sure to upload your scans immediately after you answer every question. Do
NOT wait till the end to avoid panic at the end.
2. The exam time is inclusive of time for scanning & uploading answers.
3. If using laptop + mobile for the exam, click on Open Test on laptop and click on
Scan & Upload on mobile.
4. If using laptop + mobile for the exam, when scanning and uploading from mobile,
ensure that the correct question is open on the laptop.
5. When clicking on Camera button on a smartphone for scanning and uploading, you
have 2 camera applications available to scan the answer: your phone's native camera
and an alternative Low Memory Camera. Click on the Low Memory Camera in
case your browser shows an error due to low memory.
Section: Part A
Marks per question: 1
25 of 25 question(s) in this section will be shown to examinee
Examinee should answer all 25 question(s) in this section
Q1 Difficulty Level: Easy
Knowledge Level: K1
In Java, which of the following is an example of an Object-Oriented concept?
Looping
Inheritance
File Input/Output
Variable Declaration
Q2 Difficulty Level: Easy
Knowledge Level: K1
What is the purpose of encapsulation in Java?
Protecting data by restricting access to it
Increasing code readability
Defining a class constructor
Managing threads in a multithreaded program
Q3 Difficulty Level: Easy
Knowledge Level: K1
Which keyword is used to declare a variable in Java?
Variable
Data
Declare
int
Q4 Difficulty Level: Easy
Knowledge Level: K1
What is the scope of a variable in Java?
It refers to the range of values a variable can hold.
It defines where in the memory the variable is stored.
It specifies the portion of code where the variable is accessible.
It determines the lifetime of a variable.
Q5 Difficulty Level: Easy
Knowledge Level: K1
Which Java operator is used to perform logical NOT?
&&
||
!
~
Q6 Difficulty Level: Easy
Knowledge Level: K1
Which control statement is used to create a loop that always executes at least once in Java?
for
while
do-while
switch
Q7 Difficulty Level: Easy
Knowledge Level: K1
What is the purpose of the "return" statement in Java?
To exit a loop prematurely
To define a method
To specify the data type of a variable
To return a value from a method
Q8 Difficulty Level: Easy
Knowledge Level: K1
Which keyword is used to declare a static variable in Java?
final
static
const
var
Q9 Difficulty Level: Easy
Knowledge Level: K1
What does the 'final' keyword signify when applied to a method in Java?
The method cannot be overridden.
The method cannot be called from another class.
The method is a class constructor.
The method is a static method.
Q10 Difficulty Level: Easy
Knowledge Level: K1
In Java, what type of inheritance involves a class inheriting from multiple parent classes?
Single inheritance
Multilevel inheritance
Hierarchical inheritance
Multiple inheritance
Q11 Difficulty Level: Easy
Knowledge Level: K1
What is the purpose of the 'super' keyword in Java?
It denotes a super-powered class.
It calls the parent class constructor.
It indicates an abstract method.
It is used for type casting.
Q12 Difficulty Level: Easy
Knowledge Level: K1
What is method overriding in Java?
It allows a method to have multiple implementations.
It hides the superclass method.
It changes the method's name.
It creates a new method in the subclass.
Q13 Difficulty Level: Easy
Knowledge Level: K1
In Java, which keyword is used to declare an abstract class?
class
abstract
interface
final
Q14 Difficulty Level: Easy
Knowledge Level: K1
What is the purpose of the 'throws' keyword in Java?
To throw exceptions manually
To create custom exceptions
To declare exceptions that a method may throw
To handle exceptions within a method
Q15 Difficulty Level: Easy
Knowledge Level: K1
What is a legacy class in Java?
A class designed for use with modern Java applications
A class that uses the latest Java features and libraries
A class that was part of older Java versions and is now considered outdated
A class that focuses on backward compatibility with Java 8
Q16 Difficulty Level: Easy
Knowledge Level: K1
In Java, which class is used to tokenize a string into multiple parts using a specified
delimiter?
StringTokenizer
StringParser
StringSplitter
StringDivider
Q17 Difficulty Level: Easy
Knowledge Level: K1
Which utility class is used for working with dates in Java, providing methods to parse,
format, and manipulate dates?
DateFormat
DateUtils
DateParser
Date
Q18 Difficulty Level: Easy
Knowledge Level: K1
Which layout manager in Java arranges components in a grid with rows and columns?
FlowLayout
BorderLayout
GridLayout
CardLayout
Q19 Difficulty Level: Easy
Knowledge Level: K1
Which event listener interface in Java is used for handling keyboard events, such as key
presses and key releases?
ActionListener
AdjustmentListener
KeyListener
MouseListener
Q20 Difficulty Level: Easy
Knowledge Level: K1
Which AWT control is used to create a clickable button in a Java application?
Label
Button
CheckBox
Choice
Q21 Difficulty Level: Easy
Knowledge Level: K1
An array is a sequential data structure that holds homogeneous data types. Which syntax to
initialize an array in Java is correct?
int[] array = [1,2,3]
int[] array = new array[]
int[] array = new int[5]
int{} array = [1,2,3]
Q22 Difficulty Level: Easy
Knowledge Level: K1
java
class Bank {
void bank() {
[Link]("From Bank");
}
}
class Sbi extends Bank {
void sbi() {
[Link]("SBI");
}
}
class Account extends Sbi {
void acc() {
[Link]("1234567890");
}
}
class Trade {
public static void main(String args[]) {
Account d = new Account();
[Link]();
[Link]();
[Link]();
}
}
Which kind of inheritance is depicted in the above code?
Single Level Inheritance
Multi-Level Inheritance
Hierarchical Level Inheritance
Run-Time polymorphism
Q23 Difficulty Level: Easy
Knowledge Level: K1
How many interfaces can be implemented by a single Java class?
only one
only two
any number of interfaces
cannot implement interface
Q24 Difficulty Level: Easy
Knowledge Level: K1
What is the correct syntax to extend an interface in JAVA?
interface_1 implements interface_2
interface_1 , interface_2
interface interface_1 extends interface_2
interface_1 throws interface_2
Q25 Difficulty Level: Easy
Knowledge Level: K1
What happens to the thread, if a process is terminated?
keep running
becomes idle
will be terminated
restart the working
Section: Part B
Marks per question: 5
5 of 5 question(s) in this section will be shown to examinee
Examinee should answer all 5 question(s) in this section
Q1 Difficulty Level: Easy
Knowledge Level: K1
a. How do whitespaces, identifiers, literals, comments, separators, and keywords collectively
contribute to the lexical structure of Java code? Provide examples to illustrate each
component.
Lexical Structure of Java Code
The lexical structure of Java defines how characters are combined to form tokens, which are
the basic building blocks of Java programs. The components include whitespaces, identifiers,
literals, comments, separators, and keywords. Here’s an overview of each:
1. Whitespaces: Whitespaces separate tokens in the code and improve readability. They
include spaces, tabs, and newlines.
o Example:
java
int count = 10; // Spaces between keywords, identifiers,
operators, and literals
2. Identifiers: Identifiers are names given to variables, methods, classes, etc. They must
start with a letter, underscore (_), or dollar sign ($), followed by letters or digits.
o Example:
java
int studentAge = 20;
String $message = "Hello";
3. Literals: Literals represent fixed values directly in the code. They can be numeric,
character, string, or boolean.
o Example:
java
int num = 42; // Numeric literal
char grade = 'A'; // Character literal
boolean isAvailable = true; // Boolean literal
4. Comments: Comments are used to annotate code for explanation and are ignored by
the compiler.
o Types:
Single-line comment: // This is a single-line comment
Multi-line comment:
java
/* This is a
multi-line comment */
o Example:
java
// Initialize the value of counter
int counter = 0;
5. Separators: Separators are symbols used to group and organize code, such as
parentheses (), braces {}, brackets [], semicolons ;, commas ,, and periods ..
o Example:
java
int[] numbers = {1, 2, 3}; // {} braces separate elements
6. Keywords: Keywords are reserved words in Java that have a specific meaning and
cannot be used as identifiers.
o Examples include class, public, static, if, else.
o Example:
java
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
OR
b. Explain the fundamental data types in Java, including byte, short, int, long, float, double,
char, and boolean. Provide scenarios where each of these data types is best suited for use in
programming.
Fundamental Data Types in Java
Java has eight fundamental (primitive) data types that are used to store different types of
values. Each type is suited for specific scenarios based on its size, precision, and purpose.
1. byte:
o Size: 8 bits (1 byte)
o Range: -128 to 127
o Best suited for scenarios where memory optimization is crucial, such as handling
large arrays in embedded systems.
o Example:
java
byte age = 25;
2. short:
o Size: 16 bits (2 bytes)
o Range: -32,768 to 32,767
o Suitable for scenarios where integer values are required but memory is limited. It
can be used to store values that are smaller than int but larger than byte.
o Example:
java
short populationInTown = 15000;
3. int:
o Size: 32 bits (4 bytes)
o Range: -2,147,483,648 to 2,147,483,647
o Best used for general-purpose integers. It’s the most commonly used data type for
numeric values.
o Example:
java
int salary = 50000;
4. long:
o Size: 64 bits (8 bytes)
o Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
o Suitable for scenarios requiring very large integer values, such as scientific
calculations or unique IDs.
o Example:
java
long distance = 1500000000000L; // Use 'L' to denote a long
literal
5. float:
o Size: 32 bits (4 bytes)
o Precision: Approximately 6-7 decimal digits
o Best suited for scenarios involving fractional numbers where precision isn’t critical,
like graphics programming or quick calculations.
o Example:
java
float price = 19.99f; // Use 'f' to denote a float literal
6. double:
o Size: 64 bits (8 bytes)
o Precision: Approximately 15-16 decimal digits
o Used when high precision is required, such as in financial applications or scientific
calculations.
o Example:
java
double temperature = 98.6;
7. char:
o Size: 16 bits (2 bytes)
o Stores a single 16-bit Unicode character
o Suitable for representing individual characters, such as letters, digits, or symbols.
o Example:
java
char grade = 'A';
8. boolean:
o Size: Not precisely defined (usually 1 bit)
o Possible values: true or false
o Used for scenarios involving logical conditions or flags.
o Example:
java
boolean isAdult = true;
These data types form the foundation of data handling in Java, and choosing the appropriate
type can improve efficiency and accuracy based on the use case.
Q2 Difficulty Level: Easy
Knowledge Level: K1
a. What is an array in Java, and how can you initialize an array? Provide an example of
initializing an array with a few elements of your choice.
What is an Array in Java?
An array in Java is a data structure that can hold multiple values of the same type. It is a
fixed-size container that stores elements in a contiguous block of memory, and each element
can be accessed using an index. The index is zero-based, meaning the first element is at index
0.
Array Initialization Example:
You can initialize an array in Java by specifying the type, the size, and optionally the values
it will hold.
java
// Initializing an array of integers with specific values
int[] numbers = {10, 20, 30, 40, 50};
// Initializing an array of strings
String[] fruits = {"Apple", "Banana", "Mango"};
Alternatively, you can create an array and then assign values to its elements:
java
// Create an array of size 5
int[] scores = new int[5];
// Assign values to the array
scores[0] = 85;
scores[1] = 90;
scores[2] = 78;
scores[3] = 88;
scores[4] = 92;
OR
b. Explain the different types of selection statements in Java, including 'if' and 'switch'.
Provide a code example for each type to illustrate their usage.
Different Types of Selection Statements in Java
Java provides selection statements that allow decision-making capabilities in the code. The
main selection statements are if-else and switch.
1. if Statement
The if statement allows executing a block of code based on a condition. If the condition
evaluates to true, the code inside the if block runs.
Syntax:
java
if (condition) {
// Code to be executed if condition is true
} else if (anotherCondition) {
// Code to be executed if anotherCondition is true
} else {
// Code to be executed if none of the above conditions are true
}
Code Example:
java
int number = 10;
if (number > 0) {
[Link]("The number is positive.");
} else if (number < 0) {
[Link]("The number is negative.");
} else {
[Link]("The number is zero.");
}
2. switch Statement
The switch statement is used to execute one of several blocks of code, depending on the
value of a variable. It works well when there are multiple possible discrete values.
Syntax:
java
switch (variable) {
case value1:
// Code to be executed if variable == value1
break;
case value2:
// Code to be executed if variable == value2
break;
default:
// Code to be executed if none of the above cases match
}
Code Example:
java
String day = "Monday";
switch (day) {
case "Monday":
[Link]("Start of the work week.");
break;
case "Friday":
[Link]("Last working day of the week.");
break;
case "Saturday":
case "Sunday":
[Link]("It's the weekend!");
break;
default:
[Link]("It's a regular weekday.");
}
In the switch statement example above, depending on the value of the day variable, a
specific message is printed to the console. The break statement ensures that only the
matching block runs, and the rest of the cases are ignored. If no cases match, the default
block executes.
Q3 Difficulty Level: Easy
Knowledge Level: K1
a. Describe the purpose of using try and catch blocks in Java's exception handling
mechanism. Provide an example scenario where try and catch blocks are essential for
gracefully handling runtime errors.
Purpose of Try and Catch Blocks in Java
The try and catch blocks in Java are fundamental components of the exception handling
mechanism used to catch and manage runtime errors, preventing abrupt termination of the
program and allowing for graceful recovery. The try block contains the code that might
throw an exception, while the catch block handles the exception if it occurs, allowing the
developer to specify how to respond to different types of errors.
Example Scenario
Consider a scenario where an application reads data from a file. If the file is not found, the
program would normally terminate with a runtime error. Using a try and catch block, this
error can be managed to prevent the program from crashing:
java
import [Link];
import [Link];
import [Link];
public class FileReaderExample {
public static void main(String[] args) {
try {
File file = new File("[Link]");
Scanner scanner = new Scanner(file);
while ([Link]()) {
[Link]([Link]());
}
} catch (FileNotFoundException e) {
[Link]("The file was not found. Please check the
file path and try again.");
}
}
}
In this example, if the file "[Link]" does not exist, a
FileNotFoundException is thrown. The catch block catches the exception and displays an
error message instead of abruptly terminating the program.
OR
b. Explain the significance of the 'finally' block in Java's exception handling. Provide an
example where the 'finally' block is used, and clarify how it ensures certain code is executed
regardless of whether an exception occurs or not.
Significance of the 'finally' Block in Java
The finally block in Java is used to execute code regardless of whether an exception is
thrown or caught. It is typically used to release resources such as closing file streams or
database connections, ensuring that these actions are performed even if an exception occurs.
Example with 'finally' Block
Consider a scenario where an application reads from a file and must close the file resource
after the operation, regardless of whether an error occurs:
java
import [Link];
import [Link];
import [Link];
public class FileReaderExample {
public static void main(String[] args) {
Scanner scanner = null;
try {
File file = new File("[Link]");
scanner = new Scanner(file);
while ([Link]()) {
[Link]([Link]());
}
} catch (FileNotFoundException e) {
[Link]("The file was not found.");
} finally {
if (scanner != null) {
[Link]();
[Link]("Scanner resource closed.");
}
}
}
}
In this example, the finally block ensures that the scanner resource is closed, regardless of
whether an exception is thrown. This prevents resource leaks, which is particularly important
when working with I/O streams or other resources that need explicit management.
Q4 Difficulty Level: Easy
Knowledge Level: K1
a. Explain the fundamental difference between Byte Streams and Character Streams in Java
I/O. When would you choose to use Byte Streams over Character Streams, and vice versa?
Difference Between Byte Streams and Character Streams in Java I/O
Fundamental Difference:
Byte Streams handle input and output of raw binary data, such as bytes. They use classes
like InputStream and OutputStream and are primarily used to deal with data that isn’t
text, such as images, audio, or video files.
Character Streams, on the other hand, handle input and output of character data. They use
classes like Reader and Writer and are designed to deal with text files or character data,
automatically managing character encoding and decoding.
When to Use:
Byte Streams are chosen when dealing with binary data where preserving raw byte
information is crucial, such as reading and writing image files, multimedia, or serialized data.
Character Streams are preferable when working with text data where proper encoding is
needed, such as reading or writing a text file with Unicode characters.
OR
b. What is the primary purpose of layout managers in Java, and how do they help in
designing user interfaces? Provide an example scenario where using a specific layout
manager, like BorderLayout or GridLayout, would be beneficial.
Primary Purpose of Layout Managers in Java
Purpose:
The primary purpose of layout managers in Java is to control the positioning and sizing of
components in a user interface, ensuring that they adjust appropriately when the window is
resized. Layout managers allow developers to design consistent and responsive interfaces
without manually calculating positions or sizes for each component.
Example Scenario:
Using a specific layout manager like BorderLayout can be beneficial when designing a user
interface where you want to divide the frame into five distinct regions: NORTH, SOUTH,
EAST, WEST, and CENTER. This layout is ideal for building dashboards with a header, footer,
side panels, and a central area.
GridLayout is useful when designing a calculator interface, where a grid of buttons must
be arranged in rows and columns, giving a uniform appearance to each button.
Q5 Difficulty Level: Easy
Knowledge Level: K1
a. What is the purpose of a constructor in a Java class, and what are the key characteristics
that distinguish constructors from regular methods?
Purpose of a Constructor in a Java Class: A constructor in a Java class is used to initialize
objects. It sets up the initial state of an object by assigning values to the instance variables
when an object is created. Constructors are called automatically when an object is instantiated
using the new keyword.
Key Characteristics that Distinguish Constructors from Regular Methods:
Name: The constructor has the same name as the class. Regular methods can have
any name.
No Return Type: Constructors do not have a return type, not even void. Regular
methods have return types.
Invocation: A constructor is automatically called when an instance of the class is
created. Regular methods are called explicitly by the object.
Purpose: The purpose of a constructor is to set up initial values and perform any
setup that the object needs, while regular methods are used for performing specific
actions after the object is initialized.
OR
b. Explain the concept of classes and objects in Java. Provide an example of a class and an
object, and describe how they relate to each other.
Classes and Objects in Java:
A class is a blueprint for creating objects. It defines a data type by bundling data
(fields) and methods that can act on the data. It describes the attributes and behaviors
that objects created from the class can have.
An object is an instance of a class. It represents a specific realization of the blueprint
defined by the class. An object has state (represented by fields/variables) and
behaviors (represented by methods).
Example of a Class and an Object:
java
// Define a class
public class Car {
// Attributes of a Car
String color;
String model;
int year;
// Constructor to initialize a Car object
public Car(String color, String model, int year) {
[Link] = color;
[Link] = model;
[Link] = year;
}
// Method to display car details
void displayDetails() {
[Link]("Car Model: " + model);
[Link]("Car Color: " + color);
[Link]("Car Year: " + year);
}
}
public class Main {
public static void main(String[] args) {
// Create an object of Car class
Car myCar = new Car("Red", "Toyota", 2022);
// Call method on the object
[Link]();
}
}
Explanation:
The class Car defines the structure for car objects, including the attributes color,
model, and year, along with a constructor and a method (displayDetails).
An object of the class Car is created using new Car("Red", "Toyota", 2022). The
object myCar is an instance of the class and contains its own values for color, model,
and year.
The class is like a blueprint (defines what properties and behaviors a car can have),
while myCar is a specific instance of that blueprint (a red Toyota car from 2022).
Section: Part C
Marks per question: 20
1 of 1 question(s) in this section will be shown to examinee
Examinee should answer all 1 question(s) in this section
Q1 Difficulty Level: Easy
Knowledge Level: K1
Describe the difference between an abstract class and an interface in Java. Provide examples
of scenarios where you would choose to use one over the other, and explain the benefits of
each.