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

Given: class CardBoard { Short story = 5; CardBoard go(CardBoard cb) { cb = null; return cb; } public static void main(String[] args) { CardBoard c1 = new CardBoard(); CardBoard c2 = new CardBoard(); CardBoard c3 = c1.go(c2); c1 = null; // do Stuff } } When // doStuff is reached, how many objects are eligible for GC?

  1. 0

  2. 1

  3. 2

  4. Compilation fails

  5. It is not possible to know

  6. An exception is thrown at runtime


Correct Option: C
  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

Given: 1. class Example { 2. public static void main(String[] args) { 3. Short s = 15; 4. Boolean b; 5. // insert code here 6. } 7. } Which, inserted independently at line 5, will compile? (Choose all that apply.)

  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

AI Explanation

To determine which options will compile when inserted at line 5, let's go through each option:

Option A) b = (Number instanceof s); This option will not compile because the syntax is incorrect. The correct syntax for the instanceof operator is variable instanceof Class.

Option B) b = (s instanceof Short); This option will compile because it uses the correct syntax for the instanceof operator. It checks if the variable s is an instance of the Short class.

Option C) b = s.instanceof(Short); This option will not compile because the syntax is incorrect. The instanceof operator should be used with the variable on the left side.

Option D) b = (s instanceof Number); This option will compile because it uses the correct syntax for the instanceof operator. It checks if the variable s is an instance of the Number class.

Option E) b = s.instanceof(Object); This option will compile because it uses the correct syntax for the instanceof operator. It checks if the variable s is an instance of the Object class.

Option F) b = (s instanceof String); This option will not compile because it uses the incorrect class name. The variable s is declared as a Short, so it cannot be an instance of the String class.

Therefore, the options that will compile when inserted at line 5 are B) b = (s instanceof Short); and D) b = (s instanceof Number);.

Given: 1. class Zippy { 2. String[] x; 3. int[] a [] = {{1,2}, {l}}; 4. Object c = new long [4] ; 5. Object[] d = x; 6. } What is the result?

  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

AI Explanation

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

Option A) Compilation succeeds - This option is correct because there are no syntax errors in the code provided.

Option B) Compilation fails due only to an error on line 3 - This option is incorrect. Line 3 initializes a two-dimensional int array a[][] with values {{1,2}, {l}}. The l is likely a typo and should be replaced with a valid integer value. However, this error does not cause compilation to fail.

Option C) Compilation fails due only to an error on line 4 - This option is incorrect. Line 4 declares an Object variable c and initializes it with a long array. This is a valid assignment, and compilation does not fail due to this line.

Option D) Compilation fails due only to an error on line 5 - This option is incorrect. Line 5 declares an Object array d and assigns it the value of x, which is a String array. This is a valid assignment, and compilation does not fail due to this line.

Option E) Compilation fails due to errors on lines 3 and 5 - This option is incorrect. As explained above, the errors on lines 3 and 5 do not cause compilation to fail.

Option F) Compilation fails due to errors on lines 3, 4, and 5 - This option is incorrect. As explained above, the errors on lines 3, 4, and 5 do not cause compilation to fail.

The correct answer is A) Compilation succeeds. This option is correct because there are no syntax errors in the provided code.

  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: 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
  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