Technical Interview Preparation


Technical Interview Preparation

Coming Soon...



Aptitude and Reasoning


Aptitude and Reasoning
Coming Soon...



PPL: Java Threads

JAVA THREADS

Viva Voce on Java Threads

Q1. What is the thread?
A thread is a lightweight sub process. 

Q2. What is multithreading?
Multithreading is a process of executing multiple threads simultaneously. Multithreading is used to obtain the multitasking. 

Q3. Differentiate between process and thread?
There are the following differences between the process and thread.
o A Program in the execution is called the process whereas; A thread is a subset of the process
o Process have different address space in memory, while threads contain a shared address space.
o Context switching can be faster between the threads as compared to context switching between the threads.

Q4. What do you understand by inter-thread communication?
o The process of communication between synchronized threads is termed as    inter-thread communication.

Q5. What is the purpose of wait() method in Java?
This method is used for inter-thread communication in Java. The java.lang.Object.wait() is used to pause the current thread.

Q6. What does join() method?
The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task. 

Q7. Describe the purpose of sleep() method.
The sleep() method in java is used to block a thread for a particular time, which means it pause the execution of a thread for a specific time. 

Q8. What is the synchronization?
Synchronization is the capability to control the access of multiple threads to any shared resource. 

Q9. What is shutdown hook?
The shutdown hook is a thread that is invoked implicitly before JVM shuts down. So we can use it to perform clean up the resource or save the state when JVM shuts down normally or abruptly.

Q10. Can we make the user thread as daemon thread if the thread is started?
No.

Q11. What about the daemon threads?
The daemon threads are the low priority threads that provide the background support and services to the user threads.

Q12. What is the difference between wait() and sleep() method?
The wait() method is defined in Object class.
The sleep() method is defined in Thread class.
The wait() method releases the lock.
The sleep() method doesn't release the lock.

Q13. When should we interrupt a thread?
We should interrupt a thread when we want to break out the sleep or wait state of a thread. We can interrupt a thread by calling the interrupt() throwing the InterruptedException.

Q14. What is the difference between notify() and notifyAll()?
The notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.

Q15. What is Thread Scheduler in java?
In Java, when we create the threads, they are supervised with the help of a Thread Scheduler, which is the part of JVM. Thread scheduler is only responsible for deciding which thread should be executed.

Q16. What is the volatile keyword in java?
Volatile keyword is used in multithreaded programming to achieve the thread safety, as a change in one volatile variable is visible to all other threads so one variable can be used by one thread at a time.

Q17. What do you understand by thread pool?
Java Thread pool represents a group of worker threads, which are waiting for the task to be allocated.

Q18. Is it possible to start a thread twice?
No.


MCQs on Java Threads

Q1. In java multi-threading, a thread can be created by
A. Extending Thread class
B. Implementing Runnable interface
C. Using both

Q2. Which method is called internally by Thread start() method?
A. execute()
B. run()
C. launch()

Q3. What is maximum thread priority in Java
A. 10
B. 12
C. 5

Q4. Which method must be implemented by a Java thread?
A. run()
B. execute()
C. start()

Q5. Execution of a java thread begins on which method call?
A. Start ()
B. Run ()
C. Execute ()

Q6. How many ways a thread can be created in Java multithreading?
A. 1
B. 2
C. 3

Q7. Which statements is/are correct
A. On calling Thread start () method a new thread get created.
B. Thread start () method call run () method internally
C. Both A and B.

Q8.Which method is used to make main thread to wait for all child threads
A. Join ()
B. Sleep ()
C. Wait ()

Q9. Daemon thread runs in
A. Background
B. Foreground
C. Both

Q10. Default value of a java thread is
A. 10
B. 1
C. 5



MCQs Answers

Q1. (C) 
Q2. (B)
Q3. (A)
Q4. (A)
Q5. (A)
Q6. (B)
Q7. (C)
Q8. (A)
Q9. (A)
Q10. (C)

PPL: Loops

LOOPS


MCQs on Loops

Q1. Depending on the place of control statement in loop, it can be classified as what?
(A)Entry-controlled  
(B)Exit-controlled  
(C)Both (A) and (B)

Q2. Which of the following options show the example of entry controlled loop?
(A)For loop  
(B)While loop 
(C)Both (A) and (B)

Q3. While loop is example of which loop?
(A)Entry-controlled loop 
(B)Exit-controlled loop 
(C)Process-controlled loop  

Q4. For loop is example of what?
(A)Exit-controlled loop  
(B)Entry-controlled loop 
(C)Processing controlled loop  

Q5. Do-while loop is which kind of loop?
(A)Entry-controlled  
(B)Exit-controlled  
(C)Entrance-controlled loop  
(D)Both (A) and (B)

Q6. Under which of the following condition, while loop is to be used?
(A)When the number of iteration cannot be pre-determined
(B)When loop terminating condition is to be tested before entering the loop
(C)None of these

Q7. What is done, when break statement comes in loop?
(A)To terminate loop instantly
(B)Through program control executes the next statement following the loop
(C)Repetition of loop


MCQs Answers

Q1. (C)
Q2. (C)
Q3. (A)
Q4. (B)
Q5. (B)
Q6. (C)
Q7. (B)

PPL: Exception Handling

C++ Exception Handling

#include < iostream.h>
using namespace std;

void main()
{
int x,y;
cout<<"Enter the value of x:";
cin>>a>>b;

cout<<"Enter the value of y:";
cin>>b;

try
{
if(y!=0)
{
cout<<"Result=" << x/y;
}
else
{
throw(y);
}
catch(int ex)
{
cout<<"Caught an exception which is:" << y ;
           }
}
}

OUTPUT:
Enter the value of a:
4
Enter the value of b;
0

Caught an exception which is:0

First we should know what is exception handling?
Ans. When in a program a situation occurred when program is not able to take decision or result is out of control. Than such conditions are needed to be handled to prevent our program. The reason behind such conditions is known as exceptions.

Some of the examples of exceptions are:
  • When divided by zero(o), which is an exception.
  • When doing operation of a number with a character, is also an exception.
  • Pressing a login button may time after entering credentials, could also be an exception.

So, for handling exceptions in a program which may be in C++, JAVA etc. Concept of exception handling is used.

In exception handling, try and catch blocks are used. In try, throw is also used.

The try block will execute the program, and if any exception occurs it will throw the exception and program control will entered into catch block.

The try block is always used to execute the program and catch block will always handled the exceptions thrown by try block.


MCQs on Exception Handling

Q1. The class at the top of exception class hierarchy is __________.
A. Arithmetic exception  
B. Throwable   
C. Object    
D. Exception

MCQs Answers

Q1. (B)

Related topics

Please use contact page in this website if you find anything incorrect or you want to share more information about the topic discussed above.