0% found this document useful (0 votes)
2 views7 pages

Java Practcals 2

The document provides a series of Java programming questions and answers covering various topics such as printing multiplication tables, manipulating TreeSets, comparing strings, and using built-in methods. Each question includes an explanation and a code example for clarity. Key concepts include memory management, exception handling, and the use of specific classes and methods in Java.

Uploaded by

bwalyasyvia600
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Java Practcals 2

The document provides a series of Java programming questions and answers covering various topics such as printing multiplication tables, manipulating TreeSets, comparing strings, and using built-in methods. Each question includes an explanation and a code example for clarity. Key concepts include memory management, exception handling, and the use of specific classes and methods in Java.

Uploaded by

bwalyasyvia600
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

What is the output of the following Java code that prints the multiplication
table of a number?

• Answer: B. It prints the multiplication table of the given number from 1 to 10.

• Explanation: The for loop iterates through values of i from 1 to 10, multiplying the
number by each value of i and printing the result.

• Code Example:
java
CopyEdit
int number = 5;
for (int i = 1; i <= 10; i++) {
[Link](number * i);
}

2. Which method is used to clear all elements from a TreeSet in Java?

• Answer: B. clear().

• Explanation: clear() removes all elements from a TreeSet.

• Code Example:
java
CopyEdit
TreeSet<Integer> treeSet = new TreeSet<>();
[Link](1);
[Link](2);
[Link](); // Clears all elements
[Link](treeSet); // Outputs an empty set: []

3. Which method is used to compare the contents of two strings in Java?

• Answer: C. equals().

• Explanation: The equals() method compares the actual content of str1 and str2,
returning true if they are equal.

• Code Example:
java
CopyEdit
String str1 = "Hello";
String str2 = "Hello";
if ([Link](str2)) {
[Link]("Strings are equal");
} else {
[Link]("Strings are not equal");
}

4. Which class is used to represent a directory in Java?

• Answer: C. File.

• Explanation: The File class is used to represent both files and directories in Java.

• Code Example:
java
CopyEdit
File file = new File("someDirectory");
if ([Link]()) {
[Link]("It is a directory.");
}

5. Which of the following is the correct way to reverse a string in Java?

• Answer: B. Using [Link]().

• Explanation: StringBuilder has an efficient reverse() method for reversing strings.

• Code Example:
java
CopyEdit
String str = "Hello";
StringBuilder sb = new StringBuilder(str);
[Link]();
[Link]([Link]()); // Outputs: "olleH"

6. In the sum of digits program, what is the role of the modulo operator (%)?

• Answer: B. The modulo operator (%) extracts the last digit.

• Explanation: The modulo operator is used to extract the last digit of the number
in the sum of digits calculation.

• Code Example:
java
CopyEdit
int number = 1234;
int sum = 0;
while (number > 0) {
sum += number % 10; // Adds the last digit
number /= 10; // Removes the last digit
}
[Link]("Sum of digits: " + sum); // Outputs: 10
7. Which method is used to get the total memory allocated to the JVM?

• Answer: A. totalMemory().

• Explanation: totalMemory() returns the total amount of memory allocated to the


JVM.
• Code Example:
java
CopyEdit
Runtime runtime = [Link]();
long totalMemory = [Link]();
[Link]("Total memory: " + totalMemory);

8. Which method is used to get the free memory available in the JVM after garbage
collection?

• Answer: C. freeMemory().

• Explanation: freeMemory() returns the amount of free memory available in the


JVM after garbage collection.

• Code Example:
java
CopyEdit
Runtime runtime = [Link]();
long freeMemory = [Link]();
[Link]("Free memory: " + freeMemory);

9. What happens if you compare strings using the = operator in Java?

• Answer: C. The = operator is for assignment, not comparison.

• Explanation: The = operator is used for assignment, not for comparing string
content. Use equals() for comparison.

• Code Example:
java
CopyEdit
String str1 = "Hello";
String str2 = "Hello";
if (str1 == str2) { // Incorrect: compares memory references
[Link]("Strings are equal");
}

10. Which of the following is the most commonly used sorting algorithm in Java?
• Answer: C. Quick Sort.

• Explanation: Quick Sort is commonly used due to its average-case time


complexity of O(n log n).

• Code Example:
java
CopyEdit
int[] arr = {3, 5, 1, 6, 2};
[Link](arr); // Java uses an optimized QuickSort for primitive
types
[Link]([Link](arr)); // Outputs: [1, 2, 3, 5, 6]

11. Which method is used to add elements to a TreeSet in Java?

• Answer: D. add().

• Explanation: The add() method is used to add elements to a TreeSet.

• Code Example:
java
CopyEdit
TreeSet<Integer> treeSet = new TreeSet<>();
[Link](1);
[Link](2);
[Link](treeSet); // Outputs: [1, 2]

12. Which method is used to attach an ActionListener to a component in AWT?

• Answer: A. addActionListener().

• Explanation: The addActionListener() method is used to add an action listener to


an AWT component, such as a Button.

• Code Example:
java
CopyEdit
Button button = new Button("Click me");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Button clicked");
}
});

13. What is the output of the following code using [Link]() on a decimal
number?
• Answer: C. 26.

• Explanation: [Link]() rounds a decimal number up to the nearest integer.

• Code Example:
java
CopyEdit
double x = 25.5;
double result = [Link](x);
[Link](result); // Outputs: 26.0

14. Which method is used to modify a string in StringBuilder?

• Answer: A. "Hello World".

• Explanation: [Link]() efficiently appends text to a string.

• Code Example:
java
CopyEdit
StringBuilder sb = new StringBuilder("Hello");
[Link](" World");
[Link]([Link]()); // Outputs: "Hello World"

15. Which of the following is the correct way to return an array from a method?

• Answer: D. Returning an array directly.

• Explanation: The method returns an array directly.

• Code Example:
java
CopyEdit
public static int[] returnArray() {
return new int[] {1, 2, 3};
}

16. What type of exception will be caught in the following code?

• Answer: B. It catches a NullPointerException.

• Explanation: When str is null, calling length() will throw a NullPointerException,


which is caught in the catch block.

• Code Example:
java
CopyEdit
String str = null;
try {
[Link]([Link]());
} catch (NullPointerException e) {
[Link]("Null Pointer Exception caught");
}

17. How is the try-with-resources statement used in Java?

• Answer: A. The try-with-resources statement ensures that resources are


automatically closed.

• Explanation: try-with-resources ensures that resources like BufferedReader are


automatically closed after use.

• Code Example:
java
CopyEdit
try (BufferedReader br = new BufferedReader(new FileReader("[Link]")))
{
[Link]([Link]());
} catch (IOException e) {
[Link]();
}

18. What is the logical error in the following code?

• Answer: C. The = operator is used incorrectly instead of == for comparison.

• Explanation: The code uses = for assignment instead of == for comparison.

• Code Example:
java
CopyEdit
int i = 0;
while (i = 10) { // Incorrect: uses assignment, not comparison
[Link]("Looping");
}

19. How does the super keyword work in Java?

• Answer: A. Using super to call the parent class method.

• Explanation: super is used to call the parent class method from a child class.

• Code Example:
java
CopyEdit
class Parent {
void display() {
[Link]("Parent class");
}
}

class Child extends Parent {


void display() {
[Link](); // Calls the parent class method
[Link]("Child class");
}
}

20. Which code correctly declares and instantiates a HashMap?

• Answer: A. This is the correct way to declare and instantiate a HashMap.

• Explanation: The code correctly declares and instantiates a HashMap with Integer
keys and String values.

• Code Example:
java
CopyEdit
HashMap<Integer, String> map = new HashMap<>();
[Link](1, "One");
[Link](2, "Two");

You might also like