0

programming languages Online Quiz - 20

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

The code in finally block will never get executed in the following program try { if (choice) { while (true) } else { system .exit(1): } }finally { codetocleanup(); }

  1. True

  2. False


Correct Option: A

class Dog { public static void bark() { System.out.print("woof "); }}class Alsation extends Dog { public static void bark() { }}public class Bark { public static void main(String args[]) { Dog woofer = new Dog(); Dog nipper = new Alsation(); woofer.bark(); nipper.bark(); }}What is the output

  1. woof

  2. woof woof

  3. Nothing will be printed as output

  4. None of the above


Correct Option: B

AI Explanation

To answer this question, we need to understand the concept of method overriding in Java.

In the given code, there are two classes: Dog and Alsation. The Dog class has a static method called bark() that prints "woof ". The Alsation class extends the Dog class and also has a static method called bark(). However, the Alsation class does not provide an implementation for the bark() method.

In the Bark class's main() method, two objects are created: woofer of type Dog and nipper of type Alsation. When the bark() method is called on woofer, it will call the bark() method of the Dog class, which prints "woof ". When the bark() method is called on nipper, it will call the bark() method of the Alsation class. Since there is no implementation of the bark() method in the Alsation class, it will use the implementation of the bark() method from the superclass (i.e., Dog class).

Therefore, the output will be "woof woof ".

Option B) woof woof - This option is correct because it matches the output explained above.

The correct answer is B) woof woof.

public class test08 { public test08(Object o) { System.out.println("Object"); } public test08(double[] dArray) { System.out.println("double array"); } public static void main(String[] args) { new test08(null); } }

  1. Object

  2. double array

  3. Compile Error

  4. None of the above


Correct Option: B

Storage area with associated semantics is called

  1. Class

  2. Constructor

  3. Object

  4. None


Correct Option: C

#include class NewInt { int num; public: NewInt(int n=0):num(n) {printf("cons\n");} int getInt() const {return num;} void setInt(int n){num =n;} ~NewInt(){printf("Dest\n");} }; int main() { NewInt a[5]; for(int i=0;i<=5;i++) a[i].setInt(i+10); for(int i=0;i<=5;i++) printf("%d\n",a[i].getInt()); return 0; } What does the above code snippet do?

  1. It creates an array of classes

  2. Gives compile time error

  3. It creates an array of objects

  4. It does nothing


Correct Option: C

Which of the following operators cannot be overloaded?

  1. ?:(Ternary Operator)

  2. .(member access)

  3. () (Paranthesis)

  4. ::(scope resolution)


Correct Option: A,B,D

Which of the following cannot be inherited?

  1. private member

  2. Constructor

  3. Destructor

  4. Overloaded assignment operator


Correct Option: B,C,D

What happens when the following code is compiled and run. Select the one correct answer. for(int i = 1; i < 3; i++) for(int j = 3; j >= 1; j--) assert i!=j : i;

  1. The class compiles and runs, but does not print anything.

  2. The number 1 gets printed with AssertionError

  3. The number 2 gets printed with AssertionError

  4. The number 3 gets printed with AssertionError

  5. The program generates a compilation error.


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) The class compiles and runs, but does not print anything. - This option is incorrect. The code contains an assertion statement that will throw an AssertionError if the condition specified in the assertion is false. Since the condition i != j evaluates to false when i is equal to j, an AssertionError will be thrown.

Option B) The number 1 gets printed with AssertionError - This option is correct. The code contains an assertion statement assert i != j : i;. When i is equal to j (as is the case when i is 1 and j is also 1), the assertion condition evaluates to false, and an AssertionError is thrown. The value of i (which is 1) is included in the AssertionError message.

Option C) The number 2 gets printed with AssertionError - This option is incorrect. The code does not reach the point where i is equal to 2, so this option is not possible.

Option D) The number 3 gets printed with AssertionError - This option is incorrect. The code does not reach the point where i is equal to 3, so this option is not possible.

Option E) The program generates a compilation error - This option is incorrect. The code does not contain any syntax errors, so it will compile successfully.

The correct answer is Option B) The number 1 gets printed with AssertionError. This option is correct because the assertion condition i != j evaluates to false when i is equal to j, and an AssertionError is thrown with the value of i (which is 1) included in the message.

  1. compiletime error at lines 1,2,3,4

  2. compiletime error at line 3

  3. compiletime error at line 1

  4. compiletime error at lines 3,4

  5. None of the above


Correct Option: D

class C1 { static interface I { static class C2 { } } public static void main(String a[]) { C1.I.C2 ob1=new C1.I.C2(); System.out.println("object created"); } } What is the result of attempting to compile and run the program?

  1. prints object created

  2. Compile time error

  3. Runtime Excepion

  4. None of the above


Correct Option: A

class C { public static void main(String a[]) { C c1=new C(); C c2=m1(c1); C c3=new C(); c2=c3; //6 anothermethod(); } static C m1(C ob1){ ob1 =new C(); return ob1; } } After line 6, how many objects are eligible for garbage collection?

  1. 1

  2. 2

  3. 3

  4. 4

  5. None of the above


Correct Option: B

AI Explanation

To determine the number of objects eligible for garbage collection after line 6, we need to analyze the code.

Let's break down the code and track the objects:

  1. C c1 = new C(); - This creates an object c1.
  2. C c2 = m1(c1); - This invokes the method m1 with c1 as a parameter. Inside the method, a new object ob1 is created and returned. This new object is assigned to c2. So, we have two objects c1 and ob1.
  3. C c3 = new C(); - This creates a new object c3.
  4. c2 = c3; - This assigns the object c3 to c2. Now, c2 refers to c3, and the object previously referred to by c2 (which was ob1) becomes eligible for garbage collection.
  5. anothermethod(); - This method call doesn't affect object eligibility for garbage collection.

After line 6, we have two objects (c1 and c3) that are still referenced, and one object (ob1) that is eligible for garbage collection since it is no longer referenced.

Therefore, the correct answer is B) 2 objects.

  1. True

  2. False


Correct Option: A
Explanation:

To solve this question, the user needs to know about short-circuit evaluation in programming languages, particularly in the case of the "&&" operator.

The correct answer is:

The Answer is: A

Explanation:

In programming languages, the "&&" operator represents the logical AND operation. In this operation, if the first expression (exp1) evaluates to false, then the second expression (exp2) is not evaluated because the entire expression is guaranteed to be false. This is known as short-circuit evaluation.

For example, consider the following code:

if (x != null &amp;&amp; x.isValid()) {
    // do something
}

In this code, if x is null, then the second expression (x.isValid()) will not be evaluated. This is because the entire expression (x != null && x.isValid()) is guaranteed to be false if x is null. This can improve performance and prevent errors that might occur if the second expression is evaluated when x is null.

Therefore, option A is correct because both the boolean expressions are not always evaluated when the "&&" operator is used.

  1. True

  2. False


Correct Option: A
Explanation:

To solve this question, the user needs to know about Conditional (ternary) Operator.

The conditional operator (also known as the ternary operator) is a shorthand way of expressing a conditional statement. It takes three operands and is denoted as condition ? true_expression : false_expression. The condition is evaluated first, and if it is true, the expression before the colon (:) is evaluated and its value is returned. If the condition is false, the expression after the colon is evaluated and its value is returned.

In the given statement, x is assigned the value of a if a is greater than b, and b otherwise.

Now let's substitute the given values of a and b:

x = (a &gt; b) ? a : b
x = (18 &gt; 27) ? 18 : 27

Since 18 is not greater than 27, the condition a &gt; b is false and x is assigned the value of b, which is 27.

Therefore, the statement "x = (a > b) ? a : b; then the value of x is 27, if a = 18 and b = 27" is true.

The Answer is: A

The "switch" selection structure must end with the default case.

  1. True

  2. False


Correct Option: B

AI Explanation

To answer this question, let's discuss the "switch" selection structure.

The "switch" selection structure is used to evaluate an expression and execute different code blocks based on the value of that expression. It consists of multiple "case" statements and an optional "default" case.

In the "switch" structure, each "case" statement checks if the value of the expression matches a specific value. If a match is found, the code block associated with that "case" statement is executed. If none of the "case" statements match the value of the expression, the code block associated with the "default" case is executed.

Now, let's analyze the given statement: "The 'switch' selection structure must end with the default case."

This statement is incorrect. The "switch" selection structure does not necessarily have to end with the "default" case. The "default" case is optional and can be placed anywhere within the "switch" structure. It is used to provide a default action when none of the "case" statements match the value of the expression.

Therefore, the correct answer is B) False.

- Hide questions