0

programming languages Online Quiz - 138

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

Which three form part of correct array declarations?

  1. public int a []

  2. static int [] a

  3. private int a [3]

  4. public final int [] a

  5. public [] int a

  6. private int [3] a []


Correct Option: A,B,D

Which two allow the class Thing to be instantiated using new Thing()?

  1. public class Thing {}

  2. public class Thing { public Thing(void) { } }

  3. public class Thing { public Thing() { } }

  4. public class Thing { public Thing(String s) { } }


Correct Option: A,C

Which statements are true?

  1. The default constructor initializes method variables.

  2. The default constructor has the same access as its class.

  3. The default constructor invoked the no-arg constructor of the superclass.

  4. If a class lacks a no-arg constructor, the compiler always creates a default constructor.

  5. The compiler creates a default constructor only when there are no other constructors for the class.


Correct Option: B,C,E

AI Explanation

To answer this question, let's go through each option and determine whether it is true or false:

A. The default constructor initializes method variables. This statement is false. The default constructor does not initialize method variables. It is responsible for initializing instance variables to their default values.

B. The default constructor has the same access as its class. This statement is true. The default constructor has the same access modifier as its class. For example, if a class has public access, the default constructor also has public access.

C. The default constructor invokes the no-arg constructor of the superclass. This statement is true. If a class extends another class, and the subclass does not explicitly call a superclass constructor, the default constructor of the subclass will automatically invoke the no-arg constructor of the superclass.

D. If a class lacks a no-arg constructor, the compiler always creates a default constructor. This statement is false. If a class lacks a no-arg constructor, and there are no other constructors defined, the compiler will not create a default constructor. In such cases, the class can only be instantiated using constructors with arguments.

E. The compiler creates a default constructor only when there are no other constructors for the class. This statement is true. The compiler creates a default constructor for a class only when there are no other constructors explicitly defined in the class. Once a class has any constructor defined, including a parameterized constructor, the compiler will not create a default constructor.

Therefore, the correct statements are: B. The default constructor has the same access as its class. C. The default constructor invokes the no-arg constructor of the superclass. E. The compiler creates a default constructor only when there are no other constructors for the class.

Which two cause a compiler error?

  1. int[] scores = {3, 5, 7};

  2. int [][] scores = {2,7,6}, {9,3,45};

  3. String cats[] = {“Fluffy”, “Spot”, “Zeus”};

  4. boolean results[] = new boolean [3] {true, false, true};


Correct Option: B,D

Given: 1. class A { 2. } 3. class Alpha { 4. private A myA = new A(); 5. 6. void dolt( A a ) { 7. a = null; 8. } 9. void tryIt() { 10. dolt( myA ); 11. } 12. } Which two statements are correct?

  1. Explicitly setting myA to null marks that instance to be eligible for garbage collection

  2. Private instances of A become eligible for garbage collection when instances of Alpha become eligible for garbage collection.

  3. Any call on tryIt() causes the private instance of A to be marked for garbage collection.

  4. There are no instanced of A that will become eligible for garbage collection.


Correct Option: A,B

Which two are benefits of fully encapsulating a class?

  1. Implementation details of the class are hidden.

  2. Access modifiers can be omitted on class data members.

  3. Internal operation of the class can be modified without impacting clients of that class.

  4. Code that uses the encapsulation class can access data members directly.

  5. Performance of class methods is improved.


Correct Option: A,C

How many ETs are called in a list window normally?

  1. 1

  2. 2

  3. 3

  4. 4

  5. 5

  6. None of the above


Correct Option: A

find the output:: public class HelloThread extends Thread { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new HelloThread()).start(); } }

  1. compiler error

  2. Hello from a thread!

  3. runtime error

  4. none of these


Correct Option: B

sleep() gives delay in ???

  1. millisecond

  2. nanosecond

  3. second

  4. depends on OS


Correct Option: D

what are the ways to instantiate a therad

  1. Extend the java.lang.Throwable class

  2. Extend the java.lang.Thread class

  3. Extend the java.lang.Runnable

  4. Implement the Runnable interface


Correct Option: B,D

what are places where run() is declared.

  1. Thread class

  2. Runnable Interface

  3. Applet

  4. Throwable


Correct Option: A,B

Thread t = new Thread(r); what is r here

  1. String

  2. Thread object

  3. Primitive data type variables

  4. object of a class which implements Runnable


Correct Option: A,D

what are the methods to prevent thread execution

  1. wait()

  2. sleep()

  3. stop()

  4. start()


Correct Option: A,B

what does join() do?

  1. A thread gets a default priority

  2. cause the currently running thread to move back to runnable so that a thread of the same priority can have a chance.

  3. Guaranteed to cause the current thread to stop executing for at least the specified sleep duration

  4. Guaranteed to cause the current thread to stop executing until the thread it joins with completes


Correct Option: D

The default priority of thread is ?

  1. 1

  2. 5

  3. 8

  4. 10


Correct Option: B

what are the true statement about synchronization

  1. A thread can acquire more than one lock

  2. Each object has just one lock.

  3. all methods in a class must be synchronized

  4. methods,variables can be synchronized,


Correct Option: A,B

what is preemptive multitasking.

  1. Obtain a thread's priority.

  2. A thread can voluntarily relinquish control.

  3. A thread can be preempted by a higher-priority thread.

  4. none of these


Correct Option: C

class A { A{cout<<"I am a constructor of A";} ~A{cout<<"I am a destructor of A";} } class B { A{cout<<"I am a constructor of B";} ~A{cout<<"I am a destructor of B";} }int main() { A a1; B b1; return 0; }

  1. I am a constructor of A,I am a destructor of A,I am a constructor of B,I am a destructor of B

  2. I am a constructor of A,I am a constructor of B,I am a destructor of A,I am a destructor of B

  3. I am a constructor of A,I am a constructor of B,I am a destructor of B,I am a destructor of A

  4. I am a destructor of B,I am a constructor of A,I am a destructor of A,I am a constructor of B


Correct Option: C

class A { const int k; public: void fun() { cout<

  1. k will display garbage values

  2. k will print 0, as default constructor is called.

  3. Error as no constructor is defined.

  4. Error, as main should precede with int and should return 0.


Correct Option: C

why constructor are used?

  1. To initialize the data members.

  2. To allocate memory for object.

  3. To initialize the member functions.

  4. To initialize the object.


Correct Option: B
- Hide questions