0

programming languages Online Quiz - 18

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

You have the following code in a file called Test.java class Base{ public static void main(String[] args){ System.out.println("Hello"); } } public class Test extends Base{} What will happen if you try to compile and run this?

  1. It will fail to compile.

  2. Runtime error

  3. Compiles and runs with no output.

  4. Compiles and runs printing "Hello"


Correct Option: D

What is the result of trying to compile and run the following code. public final static void main(String[] args){ double d = 10.0 / -0; if(d == Double.POSITIVE_INFINITY) System.out.println("Positive infinity"); else System.out.println("Negative infinity"); }

  1. output Positive infinity

  2. output Negative infinity

  3. Will fail to compile

  4. Runtime exception


Correct Option: A

What is the result that will be printed out ? void aMethod() { float f = (1 / 4) * 10; int i = Math.round(f); System.out.println(i); }

  1. 2

  2. 0

  3. 3

  4. 2.5

  5. 25


Correct Option: B

AI Explanation

To determine the result that will be printed out, let's go through the code step by step:

  1. The expression (1 / 4) is evaluated. Since both 1 and 4 are integers, integer division is performed. In integer division, the result is the quotient without any remainder. So, 1 / 4 will evaluate to 0.

  2. The expression 0 * 10 is evaluated. Multiplying any number by 0 will always result in 0.

  3. The variable f is assigned the value of 0.

  4. The Math.round() function is called with f as the argument. The Math.round() function rounds the floating-point value to the nearest integer. In this case, since f is 0, it remains unchanged.

  5. The value of i is assigned the rounded value of f, which is still 0.

  6. Finally, the value of i is printed using System.out.println(i). So, the output will be 0.

Therefore, the correct answer is B) 0.

Which of the following are valid declarations?

  1. int i = 0XCAFE;

  2. boolean b = 0;

  3. char c = A;

  4. byte b = 128;

  5. char c = "A";


Correct Option: A

AI Explanation

To determine which of the given options are valid declarations, let's analyze each option:

A. int i = 0XCAFE; This option is a valid declaration. In Java, the prefix "0X" indicates a hexadecimal value. The hexadecimal value 0XCAFE is a valid integer value.

B. boolean b = 0; This option is not a valid declaration. In Java, the boolean data type can only have two possible values: true or false. Assigning an integer value of 0 to a boolean variable is not allowed.

C. char c = A; This option is not a valid declaration. In Java, character literals should be enclosed in single quotes (''). The character literal 'A' represents the character 'A', so the correct declaration would be char c = 'A';.

D. byte b = 128; This option is not a valid declaration. In Java, the byte data type can hold values from -128 to 127. Assigning the value 128 to a byte variable exceeds the range of valid values.

E. char c = "A"; This option is not a valid declaration. In Java, character literals should be enclosed in single quotes (''). The double quotes ("") are used for string literals. To assign the character 'A' to a char variable, the correct declaration would be char c = 'A';.

Therefore, the valid declaration among the given options is A) int i = 0XCAFE.

What will happen if you try to compile and run this ? public class Test{ static{ print(10); } static void print(int x){ System.out.println(x); System.exit(0); } }

  1. Compiler error.

  2. Will throw a NoSuchMethod error at runtime.

  3. It will compile and run printing out "10"

  4. It will run with no output.

  5. It will run and print "10" and then crash with an error.


Correct Option: C

AI Explanation

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

The given code defines a class called "Test" with a static method called "print" that takes an integer argument.

In the static block of the class, the method "print" is called with an argument of 10.

When the code is compiled and executed, the static block will be executed before the main method. Therefore, the "print" method will be called with an argument of 10.

Inside the "print" method, the argument value is printed using System.out.println(x), which will print "10" to the console.

After printing the value, the System.exit(0) statement is encountered. This statement terminates the Java Virtual Machine (JVM) and the program execution stops.

Therefore, when the code is compiled and run, it will print out "10" and then terminate.

Based on this explanation, the correct answer is C) It will compile and run, printing out "10".

The following code is legal? long longArr[]; int intArr[] = { 7 ,8 , 9}; longArr = intArr;

  1. True

  2. False


Correct Option: B

The range of a byte is from -127 to 128.

  1. True

  2. False


Correct Option: B

Identify the invalid assignments.

  1. float f = \u0038;

  2. long L2 = 2L;

  3. float f = 1.2;

  4. char c = '/u004E';

  5. byte b = 100;


Correct Option: C

AI Explanation

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

Option A) float f = \u0038; - This option is valid. The value \u0038 is the Unicode representation of the character '8', and it can be assigned to a float variable.

Option B) long L2 = 2L; - This option is valid. The 'L' suffix indicates that the literal value 2 is of type long, and it can be assigned to a long variable.

Option C) float f = 1.2; - This option is invalid. The literal value 1.2 is a double, and it cannot be directly assigned to a float variable without an explicit typecast.

Option D) char c = '/u004E'; - This option is invalid. The character literal '/u004E' is incorrect. The correct representation of the Unicode character 'N' should be '\u004E'.

Option E) byte b = 100; - This option is valid. The literal value 100 is within the range of a byte, and it can be assigned to a byte variable.

The correct answer is C. This option is invalid because the literal value 1.2 is a double and cannot be directly assigned to a float variable without an explicit typecast.

What attributes do all real world objects have?

  1. size and weight

  2. identity, state, and behavior

  3. state and behavior

  4. height and shape


Correct Option: B
  1. initialization

  2. instantiation

  3. inheritance

  4. insubordination


Correct Option: B

Short-circuit parameters are && and ||

  1. True

  2. False


Correct Option: A

Which class is the highest superclass of Java?

  1. root

  2. object

  3. stem

  4. minor


Correct Option: B

Setter is a special method, that sets property value. Used for private or protected variables or for some additional validation

  1. True

  2. False


Correct Option: A

Which of the following is not a C# keyword?

  1. a. if

  2. b. implements

  3. c. private

  4. d. delegates


Correct Option: B
- Hide questions