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

Expressions are evaluated from right to left by default.

  1. True

  2. False


Correct Option: B

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.

class Hello{} class Hai extends Hello{} class Me{ static int x=7; static String s=null; public static void getWeight(Hello m){ int y=0/x; System.out.print(s+""); } public static void main(String [] args){ Hello [] ha={new Hello(),new Hai()}; for(Object o:ha) getWeight((Hello)o); } } When i run as--- java Me.java

  1. NoClassDefFoundError

  2. ClassCastException

  3. ArthmeticException

  4. IllegalArgumentException


Correct Option: A

class Emu { static String s = "-"; public static void main(String[] args) { try { throw new Exception(); } catch (Exception e) { try { try { throw new Exception(); } catch (Exception ex) { s += "ic "; } throw new Exception(); } catch (Exception x) { s += "mc "; } finally { s += "mf "; } } finally { s += "of "; } System.out.println(s); } }

  1. -ic mc of mf

  2. Compilation fails.

  3. -ic mc mf of

  4. -ic mf of


Correct Option: C

AI Explanation

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

Option A) -ic mc of mf - This option is incorrect. After the first catch block, the code enters the second catch block and appends "mc " to the string s. Finally, the finally block appends "mf " to the string s. Therefore, the correct sequence of statements is "-ic mc mf of".

Option B) Compilation fails - This option is incorrect. The code does not have any compilation errors.

Option C) -ic mc mf of - This option is correct. After the first catch block, the code enters the second catch block and appends "mc " to the string s. Finally, the finally block appends "mf " to the string s. Therefore, the correct sequence of statements is "-ic mc mf of".

Option D) -ic mf of - This option is incorrect. After the first catch block, the code enters the second catch block and appends "mc " to the string s. However, the finally block is executed before the end of the outer try-catch block, so "mf " is appended to the string s before the code reaches the end. Therefore, the correct sequence of statements is "-ic mc mf of".

The correct answer is Option C) -ic mc mf of. This option is correct because it accurately represents the sequence of statements executed in the code.

What is the result? class Circus { public static void main(String[] args) { int x = 9; int y = 6; for(int z = 0; z < 6; z++, y--) { if(x > 2) x--; label: if(x > 5) { System.out.print(x + " "); --x; continue label; } x--; } } }

  1. 8

  2. 8 7

  3. An exception is thrown at runtime.

  4. Compilation fails.


Correct Option: D

class Eggs { int doX(Long x, Long y) { return 1; } int doX(long... x) { return 2; } int doX(Integer x, Integer y) { return 3; } int doX(Number n, Number m) { return 4; } public static void main(String[] args) { new Eggs().go(); } void go() { short s = 7; System.out.print(doX(s,s) + " "); System.out.println(doX(7,7)); } } What is the result?

  1. 3 3

  2. 4 1

  3. 4 3

  4. 2 1


Correct Option: C

AI Explanation

To determine the result of the given code, let's go through each step:

  1. The go() method is called from the main() method.
  2. Inside the go() method, a variable s of type short is assigned a value of 7.
  3. The doX(s, s) method is called with the arguments (s, s).
    • Since there is no exact match for the arguments (short, short), the compiler looks for the closest match.
    • The closest match is the doX(Number n, Number m) method, which takes two Number objects as arguments.
    • The short values s are automatically promoted to Integer objects, which are a subclass of Number.
    • Therefore, the doX(Number n, Number m) method is called.
    • This method returns 4.
  4. The doX(7, 7) method is called with the arguments (7, 7).
    • The exact match for the arguments (Integer, Integer) is found in the doX(Integer x, Integer y) method.
    • This method returns 3.
  5. The result is printed as 4 3.

Therefore, the correct answer is C) 4 3.

class MyThread extends Thread { MyThread() { System.out.print(" MyThread"); } public void run() { System.out.print(" bar"); } public void run(String s) { System.out.print(" baz"); } } public class TestThreads { public static void main (String [] args) { Thread t = new MyThread() { public void run() { System.out.print(" foo"); } }; t.start(); } } What is the result?

  1. MyThread foo

  2. MyThread bar

  3. foo bar

  4. foo bar baz


Correct Option: A

AI Explanation

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

  1. We have a class MyThread that extends the Thread class.
  2. Inside the MyThread class, there is a constructor that prints "MyThread" when an instance of MyThread is created.
  3. The MyThread class also has two run methods. The first run method does not take any arguments and prints "bar". The second run method takes a string argument and prints "baz".
  4. We have another class called TestThreads with the main method.
  5. In the main method, a new thread t is created as an instance of MyThread with an anonymous inner class.
  6. Inside the anonymous inner class, we override the run method to print "foo".
  7. Finally, the start method is called on the t thread.

Now, let's analyze the options:

Option A) MyThread foo: This option is correct because when we call the start method on the t thread, it executes the overridden run method in the anonymous inner class, which prints "foo". The "MyThread" is printed by the constructor when the MyThread instance is created.

Option B) MyThread bar: This option is incorrect because the overridden run method in the anonymous inner class is executed, which prints "foo" instead of "bar".

Option C) foo bar: This option is incorrect because the "MyThread" is not printed. Only the overridden run method in the anonymous inner class is executed, which prints "foo" and "bar".

Option D) foo bar baz: This option is incorrect because the run method that takes a string argument is not executed. Only the overridden run method in the anonymous inner class is executed, which prints "foo" and "bar".

Therefore, the correct answer is Option A) MyThread foo.

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.

Which are methods of the Object class? (Choose all that apply.)

  1. notify();

  2. wait(long msecs);

  3. interrupt();

  4. synchronized();


Correct Option: A,B

Which method names follow the JavaBeans standard? (Choose all that apply.)

  1. getCust

  2. isColorado

  3. addSize

  4. putDimensions


Correct Option: A,B

AI Explanation

To answer this question, we need to understand the JavaBeans standard. The JavaBeans standard is a set of naming conventions for methods in Java classes that are used to represent properties.

Let's go through each option to determine which ones follow the JavaBeans standard:

Option A) getCust - This method follows the JavaBeans standard. The prefix "get" indicates that this method is used to retrieve the value of a property called "cust".

Option B) isColorado - This method also follows the JavaBeans standard. The prefix "is" is used for boolean properties, and "Colorado" is the name of the property.

Option C) addSize - This method does not follow the JavaBeans standard. The prefix "add" is typically used for methods that add elements to a collection or perform some kind of addition operation.

Option D) putDimensions - This method does not follow the JavaBeans standard. The prefix "put" is typically used for methods that add or update elements in a map or dictionary.

Therefore, the correct options that follow the JavaBeans standard are A) getCust and B) isColorado.

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. class X { void do1() { } } 2. class Y extends X { void do2() { } } 3. 4. class Chrome { 5. public static void main(String [] args) { 6. X x1 = new X(); 7. X x2 = new Y(); 8. Y y1 = new Y(); 9. // insert code here 10. } 11. } Which, inserted at line 9, will compile? (Choose all that apply.)
  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. class Dog { } 2. class Beagle extends Dog { } 3. 4. class Kennel { 5. public static void main(String [] arfs) { 6. Beagle b1 = new Beagle(); 7. Dog dog1 = new Dog(); 8. Dog dog2 = b1; 9. // insert code here 10. } 11. } Which, inserted at line 9, will compile? (Choose all that apply.)
  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.

What will be the output of the following program?

  1. 0

  2. 1

  3. Compilation Error

  4. Runtime Exception


Correct Option: C

What is the output of the program?

  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