0

programming languages Online Quiz - 178

Description: programming languages Online Quiz - 178
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0
  1. The invocation of an object's finalize() method is always the last thing that happens before an object is garbage collected (GCed).

  2. When a stack variable goes out of scope it is eligible for GC.

  3. Some reference variables live on the stack, and some live on the heap.

  4. Only objects that have no reference variables referring to them can be eligible for GC.

  5. It's possible to request the GC via methods in either java. lang. Runtime or java.lang.System classes.


Correct Option: C,E
  1. Faster f = Faster.Higher;

  2. Faster f = Better.Faster.Higher;

  3. Better.Faster f = Better.Faster.Higher;

  4. Bigger.Faster f = Bigger.Faster.Higher;

  5. Better. Faster f2; f2 = Better.Faster.Longer;

  6. Better b; b.Faster = f3; f3 = Better.Faster.Longer;


Correct Option: C,E
  1. b = (Number instanceof s);

  2. b = (s instanceof Short);

  3. b = s.instanceof(Short);

  4. b = (s instanceof Number);

  5. b = s.instanceof(Object);

  6. b = (s instanceof String);


Correct Option: B,D
  1. Compilation succeeds.

  2. Compilation fails due only to an error on line 3

  3. Compilation fails due only to an error on line 4

  4. Compilation fails due only to an error on line 5

  5. Compilation fails due to errors on lines 3 and 5

  6. Compilation fails due to errors on lines 3, 4, and 5


Correct Option: A

Given: class Fizz { int x = 5; public static void main(String[] args) { final Fizz f1 = new Fizz(); Fizz f2 = new Fizz(); Fizz f3 = FizzSwitch(f1,f2); System.out.println((f1 == f3) + " " + (f1.x == f3.x)); } static Fizz FizzSwitch(Fizz x, Fizz y) { final Fizz z = x; z.x = 6; return z; } } What is the result?

  1. true true

  2. false true

  3. true false

  4. false false

  5. Compilation fails.

  6. An exception is thrown at runtime


Correct Option: A
Explanation:

Explanation: Let's go through the code step by step to understand the result.

  1. We have a class called "Fizz" with an instance variable "x" initialized to 5.

  2. In the main method, we create two instances of the "Fizz" class - f1 and f2.

  3. We then call the static method "FizzSwitch" passing f1 and f2 as arguments. In this method, we declare a final variable "z" and assign it the value of "x" (which is f1).

  4. Next, we modify the value of "x" using the "z" reference. Therefore, the value of "x" in f1 is changed to 6.

  5. Finally, we return the reference "z" (which is f1) and assign it to the variable "f3".

  6. In the println statement, we compare if f1 and f3 are the same object reference (f1 == f3) and if their instance variable "x" is the same (f1.x == f3.x).

Now let's analyze the options:

Option A) true true - This option is correct because f1 and f3 refer to the same object, and the value of "x" in both objects is 6.

Option B) false true - This option is incorrect because f1 and f3 refer to the same object, so f1 == f3 should be true.

Option C) true false - This option is incorrect because f1 and f3 refer to the same object, so f1 == f3 should be true.

Option D) false false - This option is incorrect because f1 and f3 refer to the same object, so f1 == f3 should be true.

Option E) Compilation fails - The code does not have any compilation errors, so this option is incorrect.

Option F) An exception is thrown at runtime - The code does not have any exceptional behavior, so this option is incorrect.

The correct answer is option A) true true. This option is correct because f1 and f3 refer to the same object, and the value of "x" in both objects is 6.

Therefore, the result of the code is "true true".

The correct answer is option A.

Given: class Knowing { static final long tooth = 343L; static long doIt(long tooth) { System.out.print(++tooth + " "); return ++tooth; } public static void main(String[] args) { System.out.print(tooth + " "); final long tooth = 340L; new Knowing().doIt(tooth); System.out.println(tooth); } } What is the result?

  1. 343 340 340

  2. 343 340 342

  3. 343 341 342

  4. 343 341 340

  5. Compilation fails.

  6. An exception is thrown at runtime


Correct Option: D

AI Explanation

To answer this question, let's go through the code step by step:

  1. The class "Knowing" is defined, and it has a static final long variable named "tooth" with a value of 343L.

  2. The method "doIt" is defined, which takes a long parameter named "tooth". Inside the method, the value of "tooth" is incremented by one using the prefix increment operator (++tooth). Then, the incremented value of "tooth" is printed.

  3. In the main method, the value of the static final variable "tooth" is printed, which is 343.

  4. A new long variable named "tooth" is declared and initialized with a value of 340L. This variable is local to the main method and shadows the static final variable "tooth" defined in the class.

  5. The "doIt" method is called with the local variable "tooth" as an argument. Inside the method, the value of "tooth" is incremented again and printed.

  6. Finally, the value of the local variable "tooth" is printed again, which is still 340.

Now let's analyze the options:

Option A) 343 340 340 - This option is incorrect because the value of the local variable "tooth" is incremented inside the "doIt" method.

Option B) 343 340 342 - This option is incorrect because the value of the local variable "tooth" is not incremented before it is printed.

Option C) 343 341 342 - This option is incorrect because the value of the local variable "tooth" is not incremented before it is printed.

Option D) 343 341 340 - This option is correct because the value of the local variable "tooth" is incremented inside the "doIt" method, resulting in the output 343 341 340.

Option E) Compilation fails - This option is incorrect because there are no compilation errors in the code.

Option F) An exception is thrown at runtime - This option is incorrect because the code does not throw any exceptions.

Therefore, the correct answer is D) 343 341 340.

Given: 1. class Eco { 2. public static void main(String[] args) { 3. Eco e1 = new Eco(); 4. Eco e2 = new Eco(); 5. Eco e3 = new Eco(); 6. e3.e = e2; 7. e1.e = e3; 8. e2 = null; 9. e3 = null; 10. e2.e = el; 11. e1 = null; 12. } 13. Eco e; 14. } At what point is only a single object eligible for GC?

  1. After line 8 runs

  2. After line 9 runs.

  3. After line 10 runs.

  4. After line 11 runs.

  5. An exception is thrown at runtime.

  6. Never in this program


Correct Option: E

AI Explanation

To determine when a single object becomes eligible for garbage collection (GC), we need to understand the concept of object references and the conditions for GC.

In this program, the class Eco has a reference variable e of type Eco. The main method creates three instances of the Eco class: e1, e2, and e3.

Let's go through each line of the code and analyze the changes:

  1. Eco e; - Declares a reference variable e without initializing it.
  2. Eco e1 = new Eco(); - Creates a new object of the Eco class and assigns its reference to e1.
  3. Eco e2 = new Eco(); - Creates a new object of the Eco class and assigns its reference to e2.
  4. Eco e3 = new Eco(); - Creates a new object of the Eco class and assigns its reference to e3.
  5. e3.e = e2; - Assigns the reference of e2 to the instance variable e of the e3 object. This creates a circular reference between e2 and e3.
  6. e1.e = e3; - Assigns the reference of e3 to the instance variable e of the e1 object.
  7. e2 = null; - Sets the e2 reference variable to null. This means that the object originally referenced by e2 is no longer accessible.
  8. e3 = null; - Sets the e3 reference variable to null. This means that the object originally referenced by e3 is no longer accessible.
  9. e2.e = el; - Throws a NullPointerException. Since e2 is null, trying to access e2.e results in an exception.
  10. e1 = null; - Sets the e1 reference variable to null. This means that the object originally referenced by e1 is no longer accessible.

Based on the code execution, let's analyze when each object becomes eligible for GC:

  1. e1 becomes eligible for GC after line 11 runs because the reference to the object is set to null.
  2. e2 becomes eligible for GC after line 8 runs because the reference to the object is set to null.
  3. e3 becomes eligible for GC after line 9 runs because the reference to the object is set to null.
  4. The object referenced by e2 becomes eligible for GC after line 10 runs because it is no longer accessible.

Therefore, at the end of the program, there is no object left that is eligible for GC. Hence, the correct answer is F. Never in this program.

The given correct answer, E. An exception is thrown at runtime, is incorrect because the exception is thrown before any object becomes eligible for GC.

Given: class Clidders { public final void flipper() { System.out.print(" Flip a Clidder"); } } public class Clidlets extends Clidders { public void flipper() { System.out.print(" Flip a Clidlet"); super.flipper(); } public static void main(String [] args) { new Clidlets().flipper(); } } What is the result?

  1. Flip a Clidlet

  2. Flip a Clidder

  3. Flip a Clidder Flip a Clidlet

  4. Flip a Clidlet Flip a Clidder

  5. Compilation fails


Correct Option: E

Given:class Clidder { private final void flipper() { System.out.print(" Flip a Clidder"); }}public class Clidlet extends Clidder { public final void flipper() { System.out.print(" Flip a Clidlet"); } public static void main(String [] args) { new Clidlet().flipper(); }}What is the result?

  1. Flip a Clidlet

  2. Flip a Clidder

  3. Flip a Clidlet Flip a Clidder

  4. Flip a Clidder Flip a Clidlet

  5. Compilation fails.


Correct Option: A

Given: 1. class Comp2 { 2. public static void main(String[] args) { 3. float f1 = 2.3f; 4. float[][] f2 = {{42.Of}, {l.7f, 2.3f}, {2.6f, 2.7f}}; 5. float[] f3 = {2.7f}; 6. Long x = 42L; 7. // insert code here 8. System.out.println("true"); 9. } 10. } And the following five code fragments: F1. if (f1 == f2) F2. if (f1 == f2[2][1]) F3. if (x == f2[0][0]) F4. if (f1 == f2 [1,1]) F5. if (f3 == f2 [2]) What is true?

  1. One of them will compile, only one will be true.

  2. Two of them will compile, only one will be true.

  3. Two of them will compile, two will be true.

  4. Three of them will compile, only one will be true

  5. Three of them will compile, exactly two will be true.

  6. Three of them will compile, exactly three will be true.


Correct Option: D

Given: public class MyOuter { public static class MyInner { public static void foot) { } } } Which, if placed in a class other than MyOuter or MyInner, instantiates an instance of the nested class?

  1. MyOuter.MyInner m = new MyOuter.Mylnner();

  2. MyOuter.MyInner mi = new MyInner();

  3. MyOuter m = new Myouter(); MyOuter.MyInner mi = m.new Myouter.MyInner();

  4. MyInner mi = new MyOuter.MyInner();


Correct Option: A

Given: public class Foo { Foo() {System.out.print("foo");} class Bar{ Bar() {System.out.print("bar");} public void go() {System.out.print("hi");} } public static void main(String[] args) { Foo f = new Foo (); f.makeBar(); } void makeBar() { (new Bar() {}).go(); } } What is the result?

  1. Compilation fails.

  2. An error occurs at runtime.

  3. foobarhi

  4. barhi

  5. hi

  6. foohi


Correct Option: C

package loop; public class TestLoop5 { public static void main(String[] args) { for(;;) { System.out.println("I am in for loop"); } } }

  1. I am in for loop

  2. Infinite Loop

  3. Compiler Error

  4. Exception


Correct Option: B
- Hide questions