Tag: programming languages

Questions Related to programming languages

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