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

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

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

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