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

What is the output of the following code when compiled and run? Select one correct answer. import java.io.*; public class Question05 { public static void main(String[] args) { Question05Sub myref = new Question05Sub(); try{ myref.test(); }catch(IOException ioe){} } void test() throws IOException{ System.out.println("In Question05"); throw new IOException(); } } class Question05Sub extends Question05 { void test() { System.out.println("In Question05Sub"); } }

  1. Prints: In Question05Sub

  2. Prints: In Question05

  3. Prints:In Question05In Question05Sub

  4. The code does not compile.


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) Prints: In Question05Sub - This option is incorrect. Although the test() method in the Question05Sub class does not throw an exception, the test() method in the superclass Question05 throws an IOException and is called in the main() method. Since the IOException is not caught or declared in the main() method, the code does not compile.

Option B) Prints: In Question05 - This option is incorrect for the same reason as Option A. The code does not compile.

Option C) Prints: In Question05In Question05Sub - This option is incorrect for the same reason as Option A. The code does not compile.

Option D) The code does not compile - This option is correct. The code does not compile because the test() method in the superclass Question05 throws an IOException, but it is not caught or declared in the main() method.

The correct answer is D. The code does not compile because the test() method in the superclass throws an IOException, but it is not caught or declared in the main() method.

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.

What is the output of the following code when compiled and run? Select one correct answer. public class Question41{ public static void main(String[] args){ Object[] obj = new Object[3]; for(int i=0;i

  1. Compilation error.

  2. Prints: 1 3 4 true false true

  3. Prints: 1 2 4 false false true

  4. Prints: 3 4 true true true

  5. None of the above.


Correct Option: C

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

  1. True

  2. False


Correct Option: A

A method in a class declared as static may be invoked simply by using the name of the method alone.

  1. True

  2. False


Correct Option: B

A static method can refer to any instance variable of the class.

  1. True

  2. False


Correct Option: B

Each method in a class must have a unique name.

  1. True

  2. False


Correct Option: B
  1. Given a one dimensional array arr, what is the correct way of getting the number of elements in arr. Select the one correct answer.
  1. a. arr.length

  2. b. arr.length - 1

  3. c. arr.size

  4. d. arr.size - 1

  5. e. arr.length()


Correct Option: A

AI Explanation

To answer this question, you need to understand how to determine the number of elements in a one-dimensional array.

Option A) arr.length - This option is correct. In most programming languages, the length property is used to find the number of elements in an array. Therefore, arr.length will give you the correct number of elements in the array.

Option B) arr.length - 1 - This option is incorrect. Subtracting 1 from arr.length would give you the index of the last element in the array, not the number of elements in the array.

Option C) arr.size - This option is incorrect. The size property is not commonly used to find the number of elements in an array.

Option D) arr.size - 1 - This option is incorrect for the same reason as Option B. Subtracting 1 from arr.size would give you the index of the last element, not the number of elements.

Option E) arr.length() - This option is incorrect. In most programming languages, length() is not a valid method for finding the number of elements in an array. The correct property is length without parentheses.

The correct answer is Option A) arr.length. This option is correct because it provides the appropriate property to determine the number of elements in the array.

  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. Select all correct declarations, or declaration and initializations of an array?
  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

4 Which of the following are the java keywords?

    1. final
    1. Abstract
    1. Long
    1. static

Correct Option: A,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

8 What will happen if you attempt to compile and run the following code? Integer ten=new Integer(10); Long nine=new Long (9); System.out.println(ten + nine); int i=1; System.out.println(i + ten);

  1. a. 19 followed by 20

  2. b. 19 followed by 11

  3. c. Compile time error

  4. d. 10 followed by 1


Correct Option: A

AI Explanation

To answer this question, let's analyze each line of code and the corresponding output.

  1. Integer ten = new Integer(10);

    • This line creates an Integer object named ten with a value of 10.
  2. Long nine = new Long(9);

    • This line creates a Long object named nine with a value of 9.
  3. System.out.println(ten + nine);

    • In this line, the + operator is used to concatenate ten and nine.
    • When using the + operator with objects, the toString() method is called on each object, and the resulting strings are concatenated.
    • The toString() method for Integer and Long classes converts the objects to their corresponding string representations.
    • Therefore, the output of this line will be "19" (10 + 9).
  4. int i = 1;

    • This line creates an int variable named i with a value of 1.
  5. System.out.println(i + ten);

    • In this line, the + operator is used to add i and ten.
    • Since ten is an Integer object, it will be unboxed to its corresponding int value before the addition.
    • Therefore, the expression will be evaluated as 1 + 10, resulting in the output "11".

Based on the above analysis, the correct answer is:

A. 19 followed by 20

9 If you run the code below, what gets printed out? String s=new String("Bicycle"); int iBegin=1; char iEnd=3; System.out.println(s.substring(iBegin,iEnd));

  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