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

public class test08 { public static void main(String[] args) { try{ A a = new B(); a.show(); }catch (Exception e){ } } }class A{ void show() throws FileNotFoundException{ System.out.println("This is the super class"); }}class B extends A{ void show() throws Exception{ System.out.println("This is the inherited class"); }}What will be the output?

  1. This is the super class

  2. This is the inherited class

  3. Compile time error

  4. Run time exception


Correct Option: C

NullPointerException is a checked exception

  1. True

  2. False


Correct Option: B

If you declare an object as constant, then every member of the object would be?

  1. variable

  2. same as if in noraml object

  3. constant

  4. none


Correct Option: C

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.

Destructors can be overloaded.

  1. True

  2. False


Correct Option: B

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. interface I{ void f1(); // 1 public void f2(); // 2 protected void f3(); // 3 private void f4(); // 4 } which lines generate compile time errors?
  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

AI Explanation

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

Option A) compile time error at lines 1,2,3,4 - This option is incorrect. It suggests that there are compile time errors at all four lines, but this is not the case. There are no compile time errors at lines 1 and 2.

Option B) compile time error at line 3 - This option is incorrect. There is no compile time error at line 3. The "protected" access modifier is valid for interface methods.

Option C) compile time error at line 1 - This option is incorrect. There is no compile time error at line 1. The default access modifier is assumed if no access modifier is specified, making the method accessible to any class within the same package.

Option D) compile time error at lines 3,4 - This option is correct. There is a compile time error at line 4. The "private" access modifier is not allowed for interface methods. Additionally, there is no compile time error at line 3.

Option E) None of the above - This option is incorrect. As explained above, option D is the correct answer.

Therefore, the correct answer is D) compile time error at lines 3,4. This option is correct because the "private" access modifier is not allowed for interface methods.

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.

class base { base() { System.out.println("base"); } base(int i1) { } } class Super extends base { Super() { System.out.println("super"); super(1); } public static void main(String [] a) { base b1=new Super(); } }

  1. compile time error

  2. prints base and super

  3. prints super and base

  4. none of the above


Correct Option: A

AI Explanation

To answer this question, we need to understand the concepts of constructors and inheritance in Java.

In the given code, there are two classes: base and Super. The Super class extends the base class.

The base class has two constructors:

  1. The default constructor base() which prints "base".
  2. The parameterized constructor base(int i1).

The Super class has one constructor Super(). Inside this constructor, the super() statement is used to invoke the default constructor of the base class.

In the main method, an object b1 is created using the new keyword. The object is of type base and is assigned as a reference to the Super class.

Now, let's go through each option to determine the correct answer:

Option A) Compile time error - This option is correct. When the Super class extends the base class, it inherits the default constructor of the base class. However, when a class has a parameterized constructor and the default constructor is not explicitly defined, the default constructor is no longer available. In this case, the base class does not have an explicit default constructor, so the super() statement in the Super class constructor will result in a compile-time error.

Option B) Prints base and super - This option is incorrect because the code will not compile, and hence, nothing will be printed.

Option C) Prints super and base - This option is incorrect because the code will not compile, and hence, nothing will be printed.

Option D) None of the above - This option is incorrect because the code will not compile, and hence, nothing will be printed.

Therefore, the correct answer is A) Compile time error.

Whenever the "&&" operator is used, such as in: exp1 && exp2 where exp1 and exp2 are boolean expressions, both the boolean expressions are not always evaluated.

  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.

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

  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.

The modulus operator (%) in Java can be used only with variables of integer type.

  1. True

  2. False


Correct Option: B

AI Explanation

To answer this question, you need to understand the concept of the modulus operator (%) in Java.

The modulus operator (%) in Java calculates the remainder of dividing one number by another. It can be used with variables of integer type, but it can also be used with variables of floating-point types, such as float or double.

Therefore, the statement that the modulus operator (%) in Java can be used only with variables of integer type is incorrect.

Option A) True - This option is incorrect because the modulus operator (%) in Java can be used with variables of floating-point types as well. Option B) False - This option is correct because the modulus operator (%) in Java can be used with variables of both integer and floating-point types.

The correct answer is B) False. This option is correct because the modulus operator (%) in Java can be used with variables of integer and floating-point types.

- Hide questions