0% found this document useful (0 votes)
79 views9 pages

Java Intermediate Assessment Quiz

The document contains a Java Intermediate Assessment consisting of 30 questions divided into two parts: multiple choice questions (1-20) and programming questions (21-30). It covers various Java concepts such as identifiers, data types, constructors, garbage collection, and includes programming tasks like reading input and reversing an array. Each question is followed by the correct answer, providing a comprehensive evaluation of Java knowledge.

Uploaded by

Dipali Chormale
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)
79 views9 pages

Java Intermediate Assessment Quiz

The document contains a Java Intermediate Assessment consisting of 30 questions divided into two parts: multiple choice questions (1-20) and programming questions (21-30). It covers various Java concepts such as identifiers, data types, constructors, garbage collection, and includes programming tasks like reading input and reversing an array. Each question is followed by the correct answer, providing a comprehensive evaluation of Java knowledge.

Uploaded by

Dipali Chormale
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

Java Intermediate Assessment (30 Questions)

Part A: Multiple Choice Questions (1-20)

1. Which of the following is not a valid Java identifier?

A. _count

B. $value

C. 2ndValue

D. value2

Answer: C

2. What is the default value of a boolean variable in Java (instance variable)?

A. true

B. false

C. 0

D. null

Answer: B

3. Which keyword is used to inherit a class in Java?

A. implements

B. extends

C. inherit

D. instanceOf

Answer: B

4. Which of the following is not a primitive data type?

A. byte

B. short

C. String

D. float

Answer: C

5. Which of the following is true about constructors?


Java Intermediate Assessment (30 Questions)

A. Constructors can return a value.

B. Constructors can be static.

C. Constructors have the same name as the class.

D. Constructors can be private only.

Answer: C

6. What is the result of `10 + 20 + "30"`?

A. 3030

B. 102030

C. 30 + "30"

D. 3030 (as String)

Answer: D

7. What is the output of `[Link](5/2);` in Java?

A. 2

B. 2.5

C. 3

D. 2.0

Answer: A

8. What does JVM stand for?

A. Java Variable Method

B. Java Virtual Machine

C. Java Verified Method

D. Java Volatile Memory

Answer: B

9. Which package is automatically imported in every Java program?

A. [Link]

B. [Link]

C. [Link]
Java Intermediate Assessment (30 Questions)

D. [Link]

Answer: A

10. What will happen if the main method is made private?

A. It will run normally

B. Compile-time error

C. Runtime error

D. The program will ignore it

Answer: C

11. Which interface must be implemented to sort elements in a natural order?

A. Comparator

B. Iterable

C. Serializable

D. Comparable

Answer: D

12. Which access modifier allows access within the same package and outside via inheritance?

A. private

B. protected

C. default

D. public

Answer: B

13. What does the `final` keyword do when used with a method?

A. Prevents overriding

B. Prevents calling

C. Makes it static

D. Converts it into an abstract method

Answer: A
Java Intermediate Assessment (30 Questions)

14. Which data type takes the least memory?

A. byte

B. short

C. int

D. char

Answer: A

15. Which of the following is NOT a valid exception type?

A. IOException

B. FileNotFoundException

C. ArithmeticException

D. VirtualMemoryError

Answer: D

16. Which of the following collections implements Map?

A. ArrayList

B. HashSet

C. HashMap

D. TreeList

Answer: C

17. Which loop is guaranteed to run at least once?

A. for

B. while

C. do-while

D. enhanced-for

Answer: C

18. Which method is used to start a thread?

A. run()

B. start()
Java Intermediate Assessment (30 Questions)

C. execute()

D. begin()

Answer: B

19. What is the output of `true && false || true`?

A. true

B. false

C. Compilation error

D. None

Answer: A

20. Which statement is true about garbage collection in Java?

A. It must be manually invoked

B. Objects with no references are eligible

C. JVM never deletes objects

D. finalize() is always called

Answer: B

Part B: Programming Questions (21-30)

21. Predict the output:

public class Test {

public static void main(String[] args) {

int a = 5;

[Link](++a * 2);

Answer: 12

22. Complete the program to read a number using BufferedReader:


Java Intermediate Assessment (30 Questions)

import [Link].*;

public class ReadInput {

public static void main(String[] args) throws IOException {

// Complete here

Answer:

BufferedReader br = new BufferedReader(new InputStreamReader([Link]));

[Link]("Enter a number: ");

int num = [Link]([Link]());

[Link]("You entered: " + num);

23. What will be the output?

class Demo {

static int count = 0;

Demo() {

count++;

public static void main(String[] args) {

new Demo();

new Demo();

[Link](count);

Answer: 2

24. Identify the error:


Java Intermediate Assessment (30 Questions)

class A {

private void show() {

[Link]("A");

class B extends A {

void show() {

[Link]("B");

Answer: Method hiding occurs. B's `show()` does not override A's method since A's method is private.

25. Complete code to reverse array:

int[] arr = {1, 2, 3, 4, 5};

Answer:

for (int i = 0, j = [Link] - 1; i < j; i++, j--) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

26. Predict the output:

String s1 = "Hello";

String s2 = new String("Hello");

[Link](s1 == s2);

[Link]([Link](s2));

Answer:

false
Java Intermediate Assessment (30 Questions)

true

27. Fix the syntax error:

public class Sample {

public void main(String[] args) {

[Link]("Hello");

Answer: main method must be static: `public static void main(String[] args)`

28. Predict the output:

int x = 2;

switch (x) {

case 1:

case 2:

[Link]("Two ");

case 3:

[Link]("Three ");

break;

default:

[Link]("Default ");

Answer: Two Three

29. Check even/odd using ternary:

int num = 7;

Answer:

String result = (num % 2 == 0) ? "Even" : "Odd";


Java Intermediate Assessment (30 Questions)

[Link](result);

30. Predict the output:

int[] arr = new int[5];

[Link](arr[0]);

Answer: 0

Common questions

Powered by AI

The expression `true && false || true` in Java evaluates to `true`. First, the `&&` operator between `true` and `false` results in `false`. Then, the `||` operator examines `false || true`, which evaluates to `true` since the `||` (OR) operation is true if at least one operand is true. Hence, the overall expression evaluates to `true` .

'String' is not considered a primitive data type in Java because it is a reference type. Primitive data types, such as byte, short, int, and float, store their values directly in memory, whereas references to instances of classes like String are stored as references or pointers to the actual memory location of the data .

In Java, the `for` and `while` loops check their condition before executing the loop body. If the condition is false at the outset, the loop body is never executed. In contrast, a `do-while` loop executes its body at least once because the loop condition is evaluated after the loop body has executed. This ensures that the loop body is run at least once regardless of the condition .

The `start()` method in Java is crucial for starting a new thread of execution. When `start()` is called on a Thread object, it invokes the `run()` method in a separate call stack, effectively creating a new concurrent thread. If `run()` is called directly instead of `start()`, the code within the `run()` method executes in the current thread rather than a new one, thus not achieving parallel execution .

The `final` keyword in Java, when used with a method, prevents that method from being overridden in any subclass. This is important for ensuring the consistency and integrity of the method's behavior across all instances of the class hierarchy. By marking a method as `final`, developers can be assured that the method's implementation remains unchanged and behaves as initially intended .

Private methods in Java, such as `show()` in class A, are not accessible to subclasses, meaning they cannot be overridden. When class B extends A and defines its own `show()` method, it is not considered an override of A's method because A's `show()` is not visible to B. This results in method hiding, not method overriding, since B's `show()` is a completely separate method .

The output of `10 + 20 + "30"` in Java is "3030". This is because the expression is evaluated from left to right. First, `10 + 20` is calculated as `30` since both are integers. The result of this addition is then concatenated with the String "30", resulting in the final output of "3030" .

The outcome of the code `System.out.println(5/2);` in Java is `2`. This happens because both operands of the division are integers, resulting in integer division (truncating the fractional part) which discards the decimal portion and rounds the result towards zero, giving the final output as `2` .

In Java, the default (or package-private) access modifier allows classes, methods, and fields to be accessible only within the same package. There is no visibility outside the package unless the class or member is expressly marked public or protected. This access level is useful for encapsulating code and reducing exposure to unintended interactions across different packages .

Making the main method private in a Java program leads to a runtime error when attempting to execute the program. The `main` method serves as the entry point for JVM execution, and it must be declared public to be accessible by entities outside the class, such as the JVM itself. If it's private, the JVM cannot access it, preventing program execution .

You might also like