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 is the result of trying to compile and run this program. public class Test{ public static void main(String[] args){ int[] a = {1}; Test t = new Test(); t.increment(a); System.out.println(a[a.length - 1]); } void increment(int[] i){ i[i.length - 1]++; } }

  1. Compiler error.

  2. Compiles and runs printing out 2

  3. Compiles and runs printing out 1

  4. An ArrayIndexOutOfBounds Exception at runtime


Correct Option: B

AI Explanation

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

Option A) Compiler error - This option is incorrect. There are no syntax errors in the given code, so it will compile successfully.

Option B) Compiles and runs printing out 2 - This option is correct. The code initializes an array a with a single element 1. It then creates an instance of the Test class and calls the increment method, passing in the array a. The increment method increments the value of the last element in the array. After the method call, the value of a[a.length - 1] will be 2, and this value is printed out using System.out.println.

Option C) Compiles and runs printing out 1 - This option is incorrect. The value of a[a.length - 1] is modified to 2 inside the increment method, so it will be printed as 2, not 1.

Option D) An ArrayIndexOutOfBounds Exception at runtime - This option is incorrect. The code does not access any elements outside the bounds of the array, so it will not throw an ArrayIndexOutOfBoundsException.

The correct answer is B. It compiles and runs, printing out 2 because the increment method modifies the last element of the array.

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 is the result of trying to compile and run the following code. public static void main(String[] args){ double d = 10 / 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: D

AI Explanation

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

Option A) Output Positive infinity - This option is incorrect. The code attempts to divide 10 by 0, which results in a runtime exception, not positive infinity.

Option B) Output Negative infinity - This option is incorrect. The code attempts to divide 10 by 0, which results in a runtime exception, not negative infinity.

Option C) Will fail to compile - This option is incorrect. The code will compile without any issues.

Option D) Runtime exception - This option is correct. The code attempts to divide 10 by 0, which is not a valid arithmetic operation in Java. This will result in a runtime exception being thrown (specifically, an ArithmeticException with the message "divide by zero").

The correct answer is D) Runtime exception. This option is correct because trying to divide by zero in Java results in a runtime exception.

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

What is another name for creating an object?

  1. initialization

  2. instantiation

  3. inheritance

  4. insubordination


Correct Option: B

String strA; String strB = new String("Cheese"); How many objects have been created in the above code snippet?

  1. 3

  2. 1

  3. 2

  4. 0


Correct Option: B

Short-circuit parameters are && and ||

  1. True

  2. False


Correct Option: A

What is the name for an application changing a readable programming language into a machine-readable language?

  1. Encoder

  2. Converter

  3. Compiler

  4. Translator


Correct Option: C

Which class is the highest superclass of Java?

  1. root

  2. object

  3. stem

  4. minor


Correct Option: B

Overloading, overriding and dynamic method binding are three types of what?

  1. Inheritance

  2. Polymorphism

  3. Generalization

  4. Abstraction


Correct Option: B

AI Explanation

To answer this question, you need to understand the concepts of overloading, overriding, and dynamic method binding.

Option A) Inheritance - This option is incorrect because overloading, overriding, and dynamic method binding are not directly related to inheritance. Inheritance is a concept that allows a class to inherit properties and behaviors from another class.

Option B) Polymorphism - This option is correct because overloading, overriding, and dynamic method binding are all types of polymorphism. Polymorphism refers to the ability of an object to take on many forms or to have multiple behaviors.

Option C) Generalization - This option is incorrect because generalization is not directly related to overloading, overriding, and dynamic method binding. Generalization is a concept in object-oriented programming that allows classes to be organized into a hierarchy based on common attributes and behaviors.

Option D) Abstraction - This option is incorrect because overloading, overriding, and dynamic method binding are not directly related to abstraction. Abstraction is a concept that focuses on hiding the implementation details and exposing only essential features of an object.

The correct answer is B) Polymorphism. This option is correct because overloading, overriding, and dynamic method binding are all types of polymorphism. They allow objects to exhibit different behaviors based on the context in which they are used.

What was the first language to bring together all the features that characterize an object-oriented programming system?

  1. C++

  2. Java

  3. SmallTalk

  4. C


Correct Option: C

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