0

programming languages Online Quiz - 323

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

What is the output of the following code when compiled and run? Select one correct answer. public class Question02 { public static void main(String[] args){ int j = 017; int i = (byte)j >> 2; //line 1 System.out.println(Integer.toBinaryString(i)); //line 2 } }

  1. Prints 3

  2. Error during compilation at line 1

  3. Prints 11

  4. Prints 0


Correct Option: C

Select three correct statements about the following code. public class Question20 { public static void main(String[] args) { Question20 myref = new Question20Sub(); try{ myref.test(); }catch(Exception e){} } void test() throws Exception{ System.out.println("In Question20"); throw new Exception(); } } class Question20Sub extends Question20 { void test() { System.out.println("In Question20Sub"); } }

  1. The try-catch block that encloses myref.test(); is mandatory for the code to compile.

  2. Prints: In Question20

  3. Prints: In Question20Sub

  4. Method test() in class Question20Sub has no obligation to declare a throws clause.

  5. An exception is thrown at runtime.


Correct Option: A,C,D

AI Explanation

To analyze the given code and determine the correct statements, let's go through each option:

Option A) The try-catch block that encloses myref.test(); is mandatory for the code to compile. This statement is correct. The test() method in the Question20 class throws an exception. If the try-catch block is not included, the code will generate a compile-time error because the exception is not handled.

Option B) Prints: In Question20 This statement is incorrect. Since the test() method is overridden in the Question20Sub class, and the test() method in Question20Sub does not throw an exception, the code will execute the test() method in Question20Sub, which prints "In Question20Sub".

Option C) Prints: In Question20Sub This statement is correct. As mentioned above, the test() method in Question20Sub is executed, and it prints "In Question20Sub".

Option D) Method test() in class Question20Sub has no obligation to declare a throws clause. This statement is correct. The test() method in Question20Sub does not declare a throws clause, indicating that it does not throw any checked exceptions. In Java, subclasses are not required to declare checked exceptions that are not declared by the superclass.

Option E) An exception is thrown at runtime. This statement is incorrect. Although the test() method in Question20 throws an exception, it is caught by the try-catch block in the main method. Therefore, no exception will be thrown at runtime.

To summarize:

Option A) This option is correct because the try-catch block is mandatory for the code to compile. Option B) This option is incorrect because it prints "In Question20Sub" instead of "In Question20". Option C) This option is correct because it prints "In Question20Sub". Option D) This option is correct because the test() method in Question20Sub has no obligation to declare a throws clause. Option E) This option is incorrect because no exception is thrown at runtime.

The correct statements are A, C, and D.

All interface methods must be declared as public when implemented in a class.

  1. True

  2. False


Correct Option: A

Each method in a class must have a unique name.

  1. True

  2. False


Correct Option: B
  1. Which of these statements are legal. Select the three correct answers.
  1. a. int arr[][] = new int[5][5];

  2. b. int []arr[] = new int[5][5];

  3. c. int[][] arr = new int[5][5];

  4. d. int[] arr = new int[5][];

  5. e. int[] arr = new int[][5];


Correct Option: A,B

AI Explanation

To answer this question, we need to understand the syntax for creating multidimensional arrays in Java.

Option A) int arr[][] = new int[5][5]; - This option is correct. It declares and initializes a two-dimensional array arr with dimensions 5x5.

Option B) int []arr[] = new int[5][5]; - This option is correct. It declares and initializes a two-dimensional array arr with dimensions 5x5. It uses a different syntax to declare the array, but it is still legal.

Option C) int[][] arr = new int[5][5]; - This option is correct. It declares and initializes a two-dimensional array arr with dimensions 5x5. It uses the common syntax for declaring arrays.

Option D) int[] arr = new int[5][]; - This option is incorrect. It declares and initializes a one-dimensional array arr with a length of 5, but the second dimension is left unspecified.

Option E) int[] arr = new int[][5]; - This option is incorrect. It tries to declare a one-dimensional array arr with an unspecified length for the first dimension and a length of 5 for the second dimension, which is not allowed.

The correct answers are A, B, and C.

  1. a. String str[];

  2. b. String str[5] = new String[5];

  3. c. String str[] = new String[] {"string1", "string2", "string3", "string4", "string5

  4. d. String str[] = {"string1","string2", "string3", "string4", "string5"};


Correct Option: A,C,D

5 What will be printed when you execute the code? class A { A() { System.out.println("Class A Constructor"); } } public class B extends A { B() { System.out.println("Class B Constructor"); } public static void main(String args[]) { B b = new B(); } }

    1. Class A Constructor followed by Class B Constructor
    1. Class B Constructor followed by Class A Constructor
    1. Compile time error
    1. Run time error

Correct Option: A

6 What will happen when you compile the following code? 1. package sources; 2. public class firstjava { 3. public static void main(String args[]) 4. { 5. private int a[] ={ 4, 4 }; 6. public int b=1; 7. a[b]=b=0; 8. System.out.println("value of b & a[0] & a[1]" + b + a[0] +a[1]); 9. } 10. }

    1. Compilation error at line 2
    1. Runtime error at line 7.
    1. Compilation error at Line 7
    1. Compiles and prints 0 0 4
    1. Compiles and prints 0 4 0

Correct Option: A

7 What will be the result of attempting to compile and run the following code? abstract class MineBase { abstract void amethod(); static int i; } public class Mine extends MineBase { public static void main(String argv[]){ int[] ar=new int[5]; for(i=0;i < ar.length;i++) System.out.println(ar[i]); } }

    1. a sequence of 5 0's will be printed
    1. Error: ar is used before it is initialized
    1. Error Mine must be declared abstract
    1. IndexOutOfBoundes Error

Correct Option: C
  1. a. Bic

  2. b. ic

  3. c. icy

  4. d. error: no method matching substring(int,char)


Correct Option: B
Explanation:

To solve this question, the user needs to understand the concept of substring and character indices.

The given code creates a string object s with value "Bicycle". The variables iBegin and iEnd have been assigned the values 1 and 3, respectively. The substring method is then called on the string object s with two arguments: iBegin and iEnd. The substring method extracts a portion of the string starting at the index specified by iBegin and ending at the index specified by iEnd-1.

Option A is incorrect because the substring method with arguments (1,3) extracts the portion of the string starting at index 1 and ending at index 2. Thus, the output is "Bi".

Option B is correct because the substring method with arguments (1,3) extracts the portion of the string starting at index 1 and ending at index 2. Thus, the output is "ic".

Option C is incorrect because the substring method with arguments (1,3) extracts the portion of the string starting at index 1 and ending at index 2. Thus, the output is "ic", not "icy".

Option D is incorrect because the syntax of the substring method is correct, and there is a matching method signature that takes an int and a char.

Therefore, the answer is:

The Answer is: B. ic

11 What will the following code print out? public class Oct{ public static void main(String argv[]){ Oct o = new Oct(); o.amethod(); } public void amethod(){ int oi= 012; System.out.println(oi); } }

  1. A 12

  2. B 012

  3. C 10

  4. D 10.0


Correct Option: C
Explanation:

To solve this question, the user needs to know the basics of Java programming language. The code creates an object of the Oct class and then calls the amethod() method, which initializes an integer variable oi with 012 and prints its value to the console.

In Java, leading zero in an integer literal denotes an octal (base-8) number. Therefore, the value 012 is an octal number that represents the decimal value 10.

So, the correct answer is:

The Answer is: C. 10

- Hide questions