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

Java

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

Java

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

1. These are the rules that must be followed when writing a program.

A. syntax
B. punctuation
C. keywords
D. operators
Answer: A. syntax

2. What does AWT stand for?


A. Abstract Working Toolkit
B. All Window Tools
C. All Writing Tools
D. Abstract Window Toolkit
Answer: D. Abstract Window Toolkit

3. Every complete statement ends with a:


A. period
B. parenthesis
C. semicolon
D. ending brace
Answer: C. semicolon

4. Which of these methods can be used to know which key is pressed?


A. getActionEvent()
B. getActionKey()
C. getModifier()
D. getKey()
Answer: D. getKey()

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;

7. JVM stands for:


A. Java Variable Machine
B. Java Variable Method
C. Java Virtual Method
D. Java Virtual Machine
Answer: D. Java Virtual Machine

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

10. Which type of polymorphism is nothing but method overloading in Java?


A. Compile time polymorphism
B. Runtime polymorphism
C. Static polymorphism
D. Both A & C
Answer: D. Both A & C
11. Which class can be used to read data line by line using the readLine() method?
A. BufferedReader
B. InputStreamReader
C. DataInputStream
D. None of the above
Answer: A. BufferedReader

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

13. Which enables some interaction with the garbage collector?


A. Java.lang.ref
B. Java.lang
C. Java.lang.reflect
D. Java.math
Answer: A. Java.lang.ref

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

15. Which keyword is used by classes to implement an interface?


A. import
B. implements
C. instance of
D. none of the above
Answer: B. implements
16. How many types of controls does AWT support?
A. 7
B. 6
C. 5
D. 8
Answer: A. 7

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;

public class PositiveNegativeCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

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.

1. C. What is the difference between an Inner Class and a Sub-Class? (4 marks)

 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.

2. A. What is data encapsulation and what is its significance? (4 marks)

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.

2. B. What is an Infinite Loop? How is an Infinite Loop declared? (4 marks)

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.

Example of Infinite Loop in Java:

java
Copy code
while (true) {
// code inside loop
}

This loop will keep running forever until explicitly broken using a break statement.

3. A. Write a Java program to get all files present in a directory. (4 marks)


java
Copy code
import java.io.File;

public class ListFilesInDirectory {


public static void main(String[] args) {
File directory = new File("C:/exampleDirectory"); // specify the
directory path
File[] filesList = directory.listFiles();

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.

3. C. What is a ClassLoader in Java? (2 marks)

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:

 Bootstrap ClassLoader: Loads core Java classes.


 Extension ClassLoader: Loads classes from extensions.
 Application ClassLoader: Loads classes from the application's classpath.

4. State and explain 5 differences between ArrayList and HashSet. (5 marks)

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.

You might also like