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).
Questions
Which three form part of correct array declarations?
- public int a []
- static int [] a
- private int a [3]
- public final int [] a
- public [] int a
- private int [3] a []
Which two allow the class Thing to be instantiated using new Thing()?
- public class Thing {}
- public class Thing { public Thing(void) { } }
- public class Thing { public Thing() { } }
- public class Thing { public Thing(String s) { } }
Which statements are true?
- The default constructor initializes method variables.
- The default constructor has the same access as its class.
- The default constructor invoked the no-arg constructor of the superclass.
- If a class lacks a no-arg constructor, the compiler always creates a default constructor.
- The compiler creates a default constructor only when there are no other constructors for the class.
Which two cause a compiler error?
- int[] scores = {3, 5, 7};
- int [][] scores = {2,7,6}, {9,3,45};
- String cats[] = {“Fluffy”, “Spot”, “Zeus”};
- boolean results[] = new boolean [3] {true, false, true};
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?
- Explicitly setting myA to null marks that instance to be eligible for garbage collection
- Private instances of A become eligible for garbage collection when instances of Alpha become eligible for garbage collection.
- Any call on tryIt() causes the private instance of A to be marked for garbage collection.
- There are no instanced of A that will become eligible for garbage collection.
Which two are benefits of fully encapsulating a class?
- Implementation details of the class are hidden.
- Access modifiers can be omitted on class data members.
- Internal operation of the class can be modified without impacting clients of that class.
- Code that uses the encapsulation class can access data members directly.
- Performance of class methods is improved.
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(); } }
- compiler error
- Hello from a thread!
- runtime error
- none of these
sleep() gives delay in ???
- millisecond
- nanosecond
- second
- depends on OS
what are the ways to instantiate a therad
- Extend the java.lang.Throwable class
- Extend the java.lang.Thread class
- Extend the java.lang.Runnable
- Implement the Runnable interface
what are places where run() is declared.
- Thread class
- Runnable Interface
- Applet
- Throwable
Thread t = new Thread(r); what is r here
- String
- Thread object
- Primitive data type variables
- object of a class which implements Runnable
what are the methods to prevent thread execution
- wait()
- sleep()
- stop()
- start()
what does join() do?
- A thread gets a default priority
- cause the currently running thread to move back to runnable so that a thread of the same priority can have a chance.
- Guaranteed to cause the current thread to stop executing for at least the specified sleep duration
- Guaranteed to cause the current thread to stop executing until the thread it joins with completes
The default priority of thread is ?
- 1
- 5
- 8
- 10
what are the true statement about synchronization
- A thread can acquire more than one lock
- Each object has just one lock.
- all methods in a class must be synchronized
- methods,variables can be synchronized,
what is preemptive multitasking.
- Obtain a thread's priority.
- A thread can voluntarily relinquish control.
- A thread can be preempted by a higher-priority thread.
- none of these
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; }
- I am a constructor of A,I am a destructor of A,I am a constructor of B,I am a destructor of B
- I am a constructor of A,I am a constructor of B,I am a destructor of A,I am a destructor of B
- I am a constructor of A,I am a constructor of B,I am a destructor of B,I am a destructor of A
- I am a destructor of B,I am a constructor of A,I am a destructor of A,I am a constructor of B
class A { const int k; public: void fun() { cout<<k; } } main() { A a1; a1.fun(); }
- k will display garbage values
- k will print 0, as default constructor is called.
- Error as no constructor is defined.
- Error, as main should precede with int and should return 0.
why constructor are used?
- To initialize the data members.
- To allocate memory for object.
- To initialize the member functions.
- To initialize the object.