class TempClass { int i; public void TempClass(int j) { int i = j; } } public class C { public static void main(String[] args) { TempClass temp = new TempClass(2); } }

  1. The program has a compilation error because TempClass does not have a default constructor.

  2. The program has a compilation error because TempClass does not have a constructor with an int argument.

  3. The program compiles fine, but it does not run because class C is not public.

  4. The program compiles and runs fine.


Correct Option: B

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) The program has a compilation error because TempClass does not have a default constructor.

This option is incorrect because the TempClass class does not require a default constructor. In the code provided, a parameterized constructor is defined in the TempClass class, which takes an int argument. Therefore, a default constructor is not needed.

Option B) The program has a compilation error because TempClass does not have a constructor with an int argument.

This option is correct because the code provided attempts to create an instance of the TempClass class with an int argument (2). However, the TempClass class only defines a parameterized constructor, which does not match the provided argument. As a result, there is a compilation error because there is no constructor in the TempClass class that accepts an int argument.

Option C) The program compiles fine, but it does not run because class C is not public.

This option is incorrect because the C class does not have any impact on the compilation or execution of the code. It is not being used or referenced in any way, so the accessibility of the C class does not affect the program's behavior.

Option D) The program compiles and runs fine.

This option is incorrect because there is a compilation error in the code. As explained in option B, the TempClass class does not have a constructor that accepts an int argument, and the code attempts to create an instance of TempClass with an int argument (2). Therefore, the program will not compile successfully.

The correct answer is B. The program has a compilation error because TempClass does not have a constructor with an int argument.

Find more quizzes: