0

programming languages Online Quiz - 134

Description: programming languages Online Quiz - 134
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0

When assigning a value to a reference variable, type doesn't matter.

  1. True

  2. False


Correct Option: B

Which are most typically thrown by an API developer or an application developer as opposed to being thrown by the JVM.

  1. IllegalStateException

  2. NumberFormatException

  3. IllegalArgumentException

  4. ClassCastException


Correct Option: A,B,C
Explanation:

To answer this question, the user needs to know what each exception represents and which exceptions are most commonly thrown by API developers or application developers, rather than by the JVM.

A. IllegalStateException: This exception is usually thrown when the state of an object is not as expected. An IllegalStateException is typically thrown by an application or API developer when an object is not in the right state to perform a particular operation.

B. NumberFormatException: This exception is thrown when a program attempts to convert a string to a numeric value, but the string is not a valid number. An application or API developer might throw a NumberFormatException when expecting a string to contain an integer, but the string actually contains letters or other non-numeric characters.

C. IllegalArgumentException: This exception indicates that a method has been passed an illegal or inappropriate argument. An application or API developer might throw an IllegalArgumentException when a parameter passed into a method does not meet the expected criteria.

D. ClassCastException: This exception is thrown when an attempt is made to cast an object to a subclass of which it is not an instance. An application or API developer might throw a ClassCastException when trying to cast an object to a type that it is not compatible with.

Based on these explanations, the exception that is most typically thrown by an API developer or an application developer rather than being thrown by the JVM is C. IllegalArgumentException.

Therefore, the answer is: C. IllegalArgumentException.

public static synchronized void main(String[] args) throws InterruptedException { Thread t = new Thread(); t.start(); System.out.print("X"); t.wait(10000); System.out.print("Y"); } What is the result of this code?

  1. It prints X and never exits.

  2. The code does not compile.

  3. It prints XY with a 10-second delay between X and Y.

  4. 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 code starts by creating a new thread t and calling its start() method. This starts the thread and allows it to run concurrently with the main thread.

  2. After starting the thread, the code prints "X" using System.out.print("X").

  3. Next, the code calls the wait() method on the t thread object with a timeout of 10,000 milliseconds (10 seconds). This causes the main thread to wait for the t thread to notify it or until the timeout expires.

  4. Finally, the code prints "Y" using System.out.print("Y").

Now, let's analyze the options:

Option A) It prints X and never exits. This option is incorrect. While the code does print "X", it does not exit immediately. It waits for the t thread to notify it or until the timeout expires.

Option B) The code does not compile. This option is incorrect. The code is valid and compiles without any errors.

Option C) It prints XY with a 10-second delay between X and Y. This option is incorrect. The code does print "X", but it does not wait for exactly 10 seconds before printing "Y". The time it takes for the t thread to notify the main thread or for the timeout to expire is not guaranteed to be exactly 10 seconds.

Option D) An exception is thrown at runtime. This option is correct. The wait() method can only be called within a synchronized block or method. In the given code, the wait() method is called on the t thread object, but there is no synchronized block or method surrounding it. This will result in a java.lang.IllegalMonitorStateException being thrown at runtime.

Therefore, the correct answer is D) An exception is thrown at runtime.

  1. notify();

  2. wait(long msecs);

  3. interrupt();

  4. synchronized();


Correct Option: A,B

public abstract class AbstractTest { public int getNum() { return 45; } public abstract class Bar { public int getNum() { return 38; } } public static void main(String[] args) { AbstractTest t = new AbstractTest() { public int getNum() { return 22; } }; AbstractTest.Bar f = t.new Bar() { public int getNum() { return 57; } }; System.out.println(f.getNum() + " " + t.getNum()); } } What is the result?

  1. An exception occurs at runtime.

  2. Compilation fails.

  3. 57 22

  4. 45 57


Correct Option: D

AI Explanation

To answer this question, we need to understand the concept of overriding methods in Java.

In the given code, we have an abstract class AbstractTest which has a method getNum() that returns the value 45. It also has an inner abstract class Bar which also has a method getNum() that returns the value 38.

In the main method, we create an instance of AbstractTest using an anonymous class and override the getNum() method to return the value 22.

AbstractTest t = new AbstractTest() {
    public int getNum() {
        return 22;
    }
};

Next, we create an instance of Bar using the new keyword and the t object. We again override the getNum() method to return the value 57.

AbstractTest.Bar f = t.new Bar() {
    public int getNum() {
        return 57;
    }
};

Finally, we print the result of f.getNum() which is 57 and t.getNum() which is 22.

System.out.println(f.getNum() + " " + t.getNum());

Therefore, the output of the code is 57 22.

The correct answer is option D) 45 57.

import static java.lang.System.*; class _ { static public void main(String... _A_V) { String $ = ""; for(int x=0; ++x < _A_V.length; ) $ += _A_V[x]; out.println($); } } And the command line: java _ - A .

  1. -A.

  2. An exception is thrown at runtime.

  3. -A

  4. A


Correct Option: D
  1. (Y)x2.do2();

  2. x2.do2();

  3. ((Y)x2).do2();

  4. None of the above statements will compile


Correct Option: C
Explanation:

To solve this question, the user needs to know about inheritance, method overriding, and type casting.

In the given code, class X defines a method do1() and class Y extends class X and includes a new method do2(). In the main method, three objects are created, x1 is an object of class X, x2 is a reference of class X that points to an object of class Y, and y1 is an object of class Y.

Now let's go through each option and explain why it is right or wrong:

A. (Y)x2.do2(); This option will not compile because x2 is a reference of class X and does not have access to the method do2() of class Y. To call a method of a subclass, we need to cast the reference to that subclass. However, we cannot cast x2 to class Y because it is not an object of class Y.

B. x2.do2(); This option will not compile because the reference x2 is of class X and does not have access to the method do2(). Since x2 is a reference of class X, only the methods defined in class X or its superclasses can be called using this reference.

C. ((Y)x2).do2(); This option will compile because we are casting the reference x2 to class Y, which has access to the do2() method. Since x2 is pointing to an object of class Y, we can safely cast it to class Y. The casted reference ((Y)x2) can then access the do2() method of class Y.

D. None of the above statements will compile. This option is incorrect because option C will compile.

Therefore, the correct answer is:

The Answer is: C. ((Y)x2).do2();

  1. Beagle b2 = (Beagle) dog1;

  2. Runtime Exception

  3. Beagle b4 = dog2;

  4. None of the above statements will compile


Correct Option: A
Explanation:

To determine which statements will compile, let's analyze each option:

A. Beagle b2 = (Beagle) dog1; This statement will not compile because it is trying to cast a Dog object (dog1) to a Beagle object. However, dog1 is not an instance of Beagle, so the cast is not valid.

B. Runtime Exception This option is not applicable as it is not a valid code statement.

C. Beagle b4 = dog2; This statement will not compile because it is trying to assign a Dog object (dog2) to a Beagle variable (b4). In Java, you cannot assign an object of a superclass to a variable of its subclass type without explicit casting.

D. None of the above statements will compile This option is incorrect because statement A will compile.

Therefore, the correct answer is A) Beagle b2 = (Beagle) dog1.

class Scoop { static int thrower() throws Exception { return 42; } public static void main(String [] args) { try { int x = thrower(); } catch (Exception e) { x++; } finally { System.out.println("x = " + ++x); } } } What is the result?

  1. x = 42

  2. x = 43

  3. x = 44

  4. Compilation fails.


Correct Option: D
Explanation:

To solve this question, the user needs to understand the concepts of exceptions and the try-catch-finally block.

The thrower() method is defined to throw an Exception, but it returns the integer 42. In the main() method, we try to call thrower(), but since it throws an exception, we catch the exception in the catch block. However, the variable x is not defined outside of the try block, so attempting to increment it in the catch block will result in a compilation error.

Therefore, the correct answer is:

The Answer is: D

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 false

  2. true true

  3. false true

  4. false false


Correct Option: B

AI Explanation

To answer this question, let's go through each option:

Option A) true false - This option is incorrect. Let's analyze the code to understand why.

In the given code, we have two instances of the Fizz class, f1 and f2. The FizzSwitch method takes two Fizz objects as parameters, x and y. In the FizzSwitch method, the value of x is assigned to z, and then the value of x.x is changed to 6. Finally, z is returned.

In the main method, f1 is passed as the first parameter and f2 is passed as the second parameter to the FizzSwitch method. The returned value from the FizzSwitch method is assigned to f3.

Since z is assigned the value of x in the FizzSwitch method, and x is a reference to f1, z and f3 both reference the same object. Therefore, (f1 == f3) will evaluate to true.

Also, the value of x in f1 is changed to 6 in the FizzSwitch method. Therefore, (f1.x == f3.x) will also evaluate to true.

Option B) true true - This option is correct. As explained above, (f1 == f3) and (f1.x == f3.x) will both evaluate to true.

Option C) false true - This option is incorrect. As explained above, (f1 == f3) will evaluate to true.

Option D) false false - This option is incorrect. As explained above, (f1 == f3) will evaluate to true.

The correct answer is B) true true.

  1. 0

  2. 1

  3. Compilation Error

  4. Runtime Exception


Correct Option: C

Which of these statements are true?

  1. There can be any number of var-arg parameters in a method

  2. Only one var-arg parameter is allowed and it must be declared as last parameter in method definition.

  3. Only one var-arg parameter is allowed and it must be declared as first parameter in method definition.

  4. None of the above


Correct Option: B
- Hide questions