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

Given: 1. class Convert { 2. public static void main(String[] args) { 3. Long xL = new Long(456L); 4. long x1 = Long.valueOf("123"); 5. Long x2 = Long.valueOf("123"); 6. long x3 = xL.longValue(); 7. Long x4 = xL.longValue(); 8. Long x5 = Long.parseLong("456"); 9. long x6 = Long.parseLong("123"); 10. } 11. } Which will compile using Java 5, but will NOT compile using Java 1.4? (Choose all that apply.)

  1. Line 4

  2. Line 5

  3. Line 6

  4. Line 7

  5. Line 8

  6. Line 9


Correct Option: A,D,E

Given: 1. class Bigger { 2. public static void main(String[] args) { 3. // insert code here 4. } 5. } 6. class Better { 7. enum Faster {Higher, Longer}; 8. } Which, inserted independently at line 3, will compile? (Choose all that apply.)

  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

AI Explanation

To answer this question, let's go through each option to understand whether it will compile or not:

Option A) Faster f = Faster.Higher; This option will not compile because the enum Faster is defined inside the class Better, so it cannot be accessed directly without specifying the class name.

Option B) Faster f = Better.Faster.Higher; This option will not compile because the enum Faster is defined inside the class Better, and it cannot be accessed directly without specifying the class name.

Option C) Better.Faster f = Better.Faster.Higher; This option will compile because it correctly specifies the enum Faster and assigns one of its values (Higher) to the variable f.

Option D) Bigger.Faster f = Bigger.Faster.Higher; This option will not compile because the enum Faster is not defined inside the class Bigger. It is defined inside the class Better.

Option E) Better. Faster f2; f2 = Better.Faster.Longer; This option will compile because it declares a variable f2 of type Better.Faster and assigns one of its values (Longer) to f2 in a separate statement.

Option F) Better b; b.Faster = f3; f3 = Better.Faster.Longer; This option will not compile because it tries to assign a value to a non-existent variable b.Faster. Additionally, the variable f3 is not declared before it is used.

The correct answers are C and E. These options will compile as they correctly access the enum Faster and assign its values to variables.

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 two files: 1. class One { 2. public static void main(String[] args) { 3. int assert = 0; 4. } 5. } 1. class Two { 2. public static void main(String[] args) { 3. assert(false); 4. } 5. } And the four command-line invocations: javac -source 1.3 One.java javac -source 1.4 One.java javac -source 1.3 Two.java javac -source 1.4 Two.java What is the result? (Choose all that apply.)

  1. Only one compilation will succeed.

  2. Exactly two compilations will succeed.

  3. Exactly three compilations will succeed.

  4. All four compilations will succeed.

  5. No compiler warnings will be produced.

  6. At least one compiler warning will be produced.


Correct Option: B,F

AI Explanation

To determine the result of the given command-line invocations, let's go through each option:

Option A) Only one compilation will succeed - This option is incorrect. Both class One and class Two will compile successfully in at least one of the invocations.

Option B) Exactly two compilations will succeed - This option is correct. Both class One and class Two will compile successfully in two of the invocations.

Option C) Exactly three compilations will succeed - This option is incorrect. Only two compilations will succeed.

Option D) All four compilations will succeed - This option is incorrect. Not all four compilations will succeed.

Option E) No compiler warnings will be produced - This option is incorrect. Compiler warnings will be produced in at least one of the invocations.

Option F) At least one compiler warning will be produced - This option is correct. Compiler warnings will be produced in at least one of the invocations.

Therefore, the correct answer is B and F.

Given: class Mixer { Mixer() { } Mixer(Mixer m) { ml = m;} Mixer m1; public static void main(String[] args) { Mixer m2 = new Mixer(); Mixer m3 = new Mixer(m2); m3.go(); Mixer m4 = m3.m1; m4.go(); Mixer m5 = m2.m1; m5.go(); } void go() { System.out.print("hi "); } } What is the result?

  1. hi

  2. hi hi

  3. hi hi hi

  4. Compilation fails

  5. hi, followed by an exception

  6. hi hi, followed by an exception


Correct Option: F

AI Explanation

To answer this question, let's go through each line of the code:

  1. In the Mixer class, there are two constructors:

    • Mixer() - This is a default constructor that does not take any arguments.
    • Mixer(Mixer m) - This constructor takes a Mixer object as an argument and assigns it to the instance variable ml.
  2. Inside the main method:

    • Mixer m2 = new Mixer(); - This creates a new Mixer object called m2 using the default constructor.
    • Mixer m3 = new Mixer(m2); - This creates a new Mixer object called m3 using the constructor that takes a Mixer object as an argument. The argument m2 is passed to this constructor.
    • m3.go(); - This calls the go() method on m3, which prints "hi ".
    • Mixer m4 = m3.m1; - This assigns the value of m3.m1 (which is null because it has not been initialized) to m4.
    • m4.go(); - This calls the go() method on m4, which also prints "hi ".
    • Mixer m5 = m2.m1; - This assigns the value of m2.m1 (which is null because it has not been initialized) to m5.
    • m5.go(); - This calls the go() method on m5, which also prints "hi ".

The output of the code will be: "hi hi ".

Therefore, the correct answer is F) "hi hi, followed by an exception".

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