Java Threading and OOP Fundamentals

Test your knowledge of Java threading concepts (multitasking, synchronization, thread lifecycle, Runnable interface) and object-oriented programming principles (constructors, encapsulation, arrays, garbage collection).

19 Questions Published

Questions

Question 1 Multiple Choice (Multiple Answers)

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 []
Question 2 Multiple Choice (Multiple Answers)

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) { } }
Question 3 Multiple Choice (Multiple Answers)

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.
Question 4 Multiple Choice (Multiple Answers)

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};
Question 5 Multiple Choice (Multiple Answers)

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.
Question 6 Multiple Choice (Multiple Answers)

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.
Question 7 Multiple Choice (Single Answer)

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
Question 8 Multiple Choice (Single Answer)

sleep() gives delay in ???

  1. millisecond
  2. nanosecond
  3. second
  4. depends on OS
Question 9 Multiple Choice (Multiple Answers)

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
Question 10 Multiple Choice (Multiple Answers)

what are places where run() is declared.

  1. Thread class
  2. Runnable Interface
  3. Applet
  4. Throwable
Question 11 Multiple Choice (Multiple Answers)

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
Question 12 Multiple Choice (Multiple Answers)

what are the methods to prevent thread execution

  1. wait()
  2. sleep()
  3. stop()
  4. start()
Question 13 Multiple Choice (Single Answer)

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
Question 14 Multiple Choice (Single Answer)

The default priority of thread is ?

  1. 1
  2. 5
  3. 8
  4. 10
Question 15 Multiple Choice (Multiple Answers)

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,
Question 16 Multiple Choice (Single Answer)

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
Question 17 Multiple Choice (Single Answer)

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
Question 18 Multiple Choice (Single Answer)

class A { const int k; public: void fun() { cout<<k; } } main() { A a1; a1.fun(); }

  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.
Question 19 Multiple Choice (Single Answer)

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.