Java
Java
A. syntax
B. punctuation
C. keywords
D. operators
Answer: A. syntax
5. Which of these packages contains all the classes and methods required for event
handling in Java?
A. java.applet
B. java.awt
C. java.awt.event
D. java.event
Answer: C. java.awt.event
6. Which of the following are not valid assignment statements? (Indicate all that apply)
A. total = 9;
B. 72 amount;
C. profit = 129
D. letter = 'W;
Answer: B. 72 amount; and D. letter = 'W;
8. Which is the container that doesn't contain title bar and MenuBars but it can have
other components like button, textfield, etc.?
A. window
B. frame
C. panel
D. container
Answer: C. panel
9. Which is used to store data and partial results, as well as to perform dynamic
linking, return values for methods, and dispatch exceptions?
A. Container
B. Frame
C. Panel
D. Window
Answer: A. Container
12. Which block contains a block of program statements where an exception might
occur?
A. catch
B. try
C. throw
D. final
Answer: B. try
14. Which class is used to create servers that listen for either local client or remote
client programs?
A. ServerSockets
B. httpServer
C. httpResponse
D. None of the above
Answer: A. ServerSockets
17. Which method of DataInputStream class reads a line from the file and returns it as
a string?
A. WriteInt()
B. readLine()
C. readInt()
D. WriteDouble()
Answer: B. readLine()
18. What is correct syntax for the main method of a Java class?
A. public static int mainString[]args
B. public int mainString[]args
C. public static void main(String[] args)
D. public static void mainString[]args
Answer: C. public static void main(String[] args)
19. Which keyword is used for the block to handle the exceptions generated by try block?
A. Try
B. Final
C. Catch
D. Throw
Answer: C. Catch
20. Which method is used for an SQL statement that is executed frequently?
A. PrepareStatement
B. prepareCall
C. createStatement
D. None of the above
Answer: A. PrepareStatement
1. A. Write a Java program to check if a number is Positive or Negative. (5
marks)
import java.util.Scanner;
if (number > 0) {
System.out.println(number + " is Positive.");
} else if (number < 0) {
System.out.println(number + " is Negative.");
} else {
System.out.println("The number is Zero.");
}
}
}
1. B. List and define the various Access Specifiers for Java Classes. (2 marks)
Public: Allows access to the class, method, or field from any other class.
Private: Restricts access to the class, method, or field only within the same class.
Protected: Allows access within the same package and to subclasses.
Default (no specifier): Access is limited to the same package.
Inner Class: A class that is defined within another class. It has access to all members
(even private) of the outer class.
Sub-Class: A class that inherits from another class (superclass). It can access protected
and public members of the superclass but not private members.
Data encapsulation is the process of wrapping the data (variables) and the code (methods) that
operates on the data into a single unit, known as a class. Encapsulation is achieved by making
variables private and providing public getter and setter methods. Its significance lies in:
Protecting the data from outside interference and misuse.
Achieving modularity and separation of concerns.
Making the code more maintainable and flexible.
An infinite loop is a loop that runs indefinitely without a terminating condition. It keeps
executing because the condition controlling the loop never becomes false.
java
Copy code
while (true) {
// code inside loop
}
This loop will keep running forever until explicitly broken using a break statement.
if (filesList != null) {
for (File file : filesList) {
System.out.println(file.getName());
}
} else {
System.out.println("Directory does not exist or is empty.");
}
}
}
3. B. Can we override the private method in Java? Give reason for your answer.
(4 marks)
No, private methods cannot be overridden in Java. This is because private methods are not
accessible outside the class they are defined in, and overriding requires access to the method in a
subclass. Private methods are not inherited by subclasses, hence they cannot be overridden.
A ClassLoader in Java is a part of the Java Runtime Environment (JRE) that dynamically loads
Java classes into memory when they are needed. It is responsible for finding and loading class
files. There are different types of class loaders, such as:
1. Order of Elements:
o ArrayList: Maintains insertion order.
o HashSet: Does not guarantee any specific order of elements.
2. Duplicates:
o ArrayList: Allows duplicate elements.
o HashSet: Does not allow duplicate elements.
3. Implementation:
o ArrayList: Implements the List interface.
o HashSet: Implements the Set interface.
4. Null Values:
o ArrayList: Can store multiple null values.
o HashSet: Can store at most one null value.
5. Performance:
o ArrayList: Provides fast access to elements via index but is slower in search and
removal.
o HashSet: Faster in search, insertion, and deletion due to hashing but slower in
iterating over elements compared to ArrayList.