Practice Questions For Java
Practice Questions For Java
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
Two threads cannot simultaneously enter into the methods of the same B 1 static synchronized synchronize synchronization
120 4
object if the methods are
121 4 Which mathod is not a part of multi-threading? A 1 Sleep() join() wait() run()
122 4 mathods used for inter thread communication are? D 1 wait() notify() notifyAll() All of the above
Which of these are types of multitasking? C 1 Process based Thread based Process and Thread based None of the mentioned
123 4
If there occurs any exception in thread, then other threads C 1 stop executing gets impacted doesn't gets impacted daemon thread starts
124 4
executing
Which mathod is used to assign name of thread by programmer? D 1 run() start() getName() setName(String s)
125 4
Which of these method waits for the thread to terminate? A 1 join() stop() sleep() isAlive()
126 4
Which of the following method is not available in Thread class? D 1 join() run() sleep() Run()
127 4
Which function of pre defined class Thread is used to check weather C 1 Join() Alive() isAlive() isRunning()
128 4
current thread being checked is still running?
Which of these method is used to tell the calling thread to give up a A 1 wait() sleep() notifyAll() notify()
129 4 monitor and go to sleep until some other thread enters the same
monitor?
What is true about threading? D 1 run() method creates new run() method calls start() run() method can be called start() method creates new
thread method and runs the code directly without start() thread and calls code
130 4 method being called written in run() method
Thread synchronization in a process will be required when D 1 All threads sharing the All threads sharing the All threads sharing the All
131 4 same address space same global variables same files
Which of the following is a correct way to synchronize a method in Java A 1 public synchronized void public void method() { public void synchronized public void method() {
132 4 code? method() { } synchronized { } } method() { } synchronized void() { } }
Answer the Question : start() method is called to ______ ? A 1 Invoke the thread, before Execute the thread which Transit the thread from Transit from runnable to
the run() method is is in 'ready to run' state 'running' to 'ready to run' waiting state
133 4
executed state
134 4 Threads in java share the same memory space and resources. B 1 FALSE TRUE Depends on Compiler Depends on JVM
L.J Institute of Engineering and Technology, Ahmedabad.
Computer Programming using Java-II PRACTICE BOOK (SEM-II-EVEN_2025-CE RELATED BRANCHES)
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
We can create thread in java by B 1 a & b only b & d only a & e only c & e only
a) Implementing Thread Class
b) Implementing Runnable Interface
135 4 c) Implementing Thread Interface
d) Extending Thread class
e) Extending Runnable class
136 4 The tasks or job that thread needs to perform is written inside D 1 inner class Static Block Constructor run()
Which is the correct Thread Class constructor? C 1 A & B only C & D only A & C Only A & D Only
A) Thread(Runnable r)
137 4 B) Thread(Runnable r, Thread t)
C) Thread(Runnable r, String s)
D) Thread(String s, Runnable r)
What is the name of the thread in output of this program? A 1 main java system thread
Class A
{
public static void main(String args[])
138 4 {
Thread t = Thread.currentThread();
System.out.println(t.getName());
}
}
what is output of this code? B 1 Hello Hii HelloHii None of the above
class A extends Thread {
public void run() {
System.out.println("Hello");
}
public void start() {
System.out.println("Hii");
139 4 }
}
class LJU {
public static void main(String[] args) {
A a = new A();
a.start();
}
}
L.J Institute of Engineering and Technology, Ahmedabad.
Computer Programming using Java-II PRACTICE BOOK (SEM-II-EVEN_2025-CE RELATED BRANCHES)
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
what is the output of this code? D 1 Hello Hii HelloHii Error
class A extends Thread {
public void run() {
System.out.println("Hello");
}
public void run(int a) {
System.out.println("Hii");
140 4 }
}
class LJU {
public static void main(String[] args) {
A a = new A();
a.start(10);
}
}
class multithreaded_programing { C 1 Thread[0,main] Thread[t,5,main] Thread[main,5,main] Thread[t1,5,main]
public static void main(String args[]) {
Thread t = Thread.currentThread();
Thread t1 = t;
141 4
System.out.println(t1);
}
}
Assume the following method is properly synchronized and called from a C 1 Two seconds after thread Two seconds after lock B is After thread A is notified After the lock on B is
thread A on an object B: A is notified released or after two seconds. released and after two
142 4 wait(2000); seconds.
After calling this method, when will the thread A become a candidate to
get another turn at the CPU?
Which of these statement is incorrect? D 1 start() method is used to A thread can be formed by A thread can be formed by run() method is used to
begin execution of the a class that extends thread implementing Runnable begin execution of a
143 4 thread class interface only thread before start()
method in special cases
What will be the output of the following code? A 1 0..1..2.. 0..1..2..3.. 1..2..3.. Compilation fails
class LJ extends Thread {
public static void main(String[] args) {
LJ t = new LJ();
Thread x = new Thread(t);
144 4 x.start(); /* Line 7 */
}
public void run() {
for (int i = 0; i < 3; ++i) {
System.out.print(i + ".."); } }
}
L.J Institute of Engineering and Technology, Ahmedabad.
Computer Programming using Java-II PRACTICE BOOK (SEM-II-EVEN_2025-CE RELATED BRANCHES)
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
Which of the following line of code is suitable to start a thread ? C 1 Thread t = new Thread(X); Thread t = new Thread(X); X run = new X(); Thread t = new Thread();
class X implements Runnable t.start(); Thread t = new x.run();
{ Thread(run); t.start();
public static void main(String args[])
{
145 4
/* Missing code? */
}
public void run() {}
}
What will be the output of the follwing code? B 1 Compile time error Run time exception Thread one. Thread two. one. two. Thread
class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread();
t.start();
146 4 System.out.print("one. ");
t.start();
System.out.print("two. ");
}
public void run() {
System.out.print("Thread ");
}
}
What is output of this code? B 1 Hello Hii HelloHii None of the above
class A extends Thread {
public void run() {
System.out.println("Hello");
}
public void run(int a) {
System.out.println("Hii");
147 4 }
}
class LJU {
public static void main(String[] args) {
A a = new A();
a.run(10);
}
}
What is the difference between synchronized methods and synchronized C 1 Synchronized methods can Synchronized methods are Synchronized blocks can There is no difference
blocks in Java? be used on non-static less efficient than be used to synchronize between synchronized
methods, while synchronized blocks access to specific parts of a methods and synchronized
148 4 synchronized blocks can method, while blocks
only be used on static synchronized methods lock
methods the entire method
L.J Institute of Engineering and Technology, Ahmedabad.
Computer Programming using Java-II PRACTICE BOOK (SEM-II-EVEN_2025-CE RELATED BRANCHES)
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
What is the output? C 1 ONE TWO main No Output
class TestDemo{
public static void main(String args[])
{
Thread t = new Thread(“ONE”);
149 4 Thread t2 = new Thread(“TWO”);
Thread t3 = Thread.currentThread();
System.out.println(t3.getName());
}
}
class newthread extends Thread { C 1 TRUE FALSE Main thread interrupted None of the mentioned
Thread t;
String name;
newthread(String threadname) {
name = threadname;
t = new Thread(this, name);
t.start(); }
public void run() {}
}
class LJ {
public static void main(String args[]) {
newthread obj1 = new newthread("one");
150 4 newthread obj2 = new newthread("two");
try {
obj1.t.wait();
System.out.print(obj1.t.isAlive());
} catch (Exception e) {
System.out.print("Main thread interrupted");
}
}
}
what is output of Below Java Programs? A 1 Thread is running no Thread is running no Thread is running no None of the above
public class ThreadExtended extends Thread { Thread is running no Thread is running no
public void run() { Thread is running no
System.out.println("Thread is running no");
}
public static void main(String[] args)
151 4 {
ThreadExtended threadE = new ThreadExtended();
ThreadExtended threadEx = new ThreadExtended();
ThreadExtended threadEx1 = new ThreadExtended();
threadEx.start();
}
}
L.J Institute of Engineering and Technology, Ahmedabad.
Computer Programming using Java-II PRACTICE BOOK (SEM-II-EVEN_2025-CE RELATED BRANCHES)
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
what is output of Below Java Programs? D 1 4 2 Thread-0 Thread-1
public class ThreadExtended extends Thread {
public void run() {
System.out.println(ThreadExtended.currentThread().getName());
}
public static void main(String[] args)
{
152 4 ThreadExtended threadE = new ThreadExtended();
ThreadExtended threadEx = new ThreadExtended();
ThreadExtended threadEx1 = new ThreadExtended();
threadEx.start();
}
}
The static method Thread.currentThread() returns a reference to the D 1 Each String in the array Each String in the array Each String in the array This code will not compile.
currently executing Thread object. What is the result of this code? lines will output, and there lines will output, with no lines will output, with a 1-
class Test { is no guarantee there will pause in between because second pause.
public static void main(String [] args) { be a pause because this method is not
printAll(args); currentThread() may not executed in a Thread.
} retrieve this thread.
public static void printAll(String[] lines) {
153 4 for(int i = 0; i < lines.length; i++)
{
System.out.println(lines[i]);
Thread.currentThread().sleep(1000);
}
}
}
What is the output of the following code? B 1 HelloHi Hi Hello compile time error output will be different
every time
class LJU extends Thread {
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
What will be the output of the following code? B 1 Compile time error 0 1 0 0 null null
class One extends Thread {
public void run(){
for(int i=0; i<2; i++)
{
System.out.print(i+" ");
}
}
}
155 4 class Test{
public static void main(String args[])
{
Test t = new Test();
One o = new One();
t.call(o);
}
public void call(One o) {
o.start();
}}
What will be the output of the following code? A 1 LJU “LJU” LJ No Output
class LJ extends Thread {
public void run() {
try {
Thread.sleep( 'a' );
} catch (Exception e) {
e.printStackTrace();
}
System.out.print("LJ"); } }
156 4 class runLJ {
synchronized public static void main(String[] args) throws Exception {
LJ t1 = new LJ( );
t1.start();
synchronized(t1) {
t1.wait();
}
System.out.println("U"); } }
L.J Institute of Engineering and Technology, Ahmedabad.
Computer Programming using Java-II PRACTICE BOOK (SEM-II-EVEN_2025-CE RELATED BRANCHES)
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
What will be the output of the following code? D 1 Q100 70Q100 a3 100
class LJIET extends Thread {
public void runn() {
System.out.print('A' + 5);
display();
}
static void display() {
157 4 System.out.print("Q");
}}
class runLJ {
public static void main(String[] args) throws Exception {
LJIET t1 = new LJIET( );
t1.start(); t1.join();
System.out.println('a' + 3);
}}
What will be the output of the following code? A 1 mAB ABm XAB Compile time error
class thread implements Runnable {
static Thread m;
public void run(){
try {
m.join( ); }
catch ( Exception e ) {
158 4 System.out.print("X"); }
System.out.print("AB"); } }
class runLJ {
public static void main(String[] args) throws Exception {
Thread t1 = new Thread( new thread ( ) , "Y");
thread.m = Thread.currentThread();
t1.start(); t1.join(1000);
System.out.print("m"); } }
What is the output of the Java code snippet? C 1 pop pop pop pop push push Compile Error Runtime Exception
class Thread implements Runnable {
public static void main(String[] args) {
Thread t = new Thread();
t.start();
}
public void run() {
159 4 for (int i = 0; i < 2; ++i) {
System.out.print("pop "); }
}
public void run(int number) {
for (int i = 0; i < number; ++i) {
System.out.print("push "); }
}}
L.J Institute of Engineering and Technology, Ahmedabad.
Computer Programming using Java-II PRACTICE BOOK (SEM-II-EVEN_2025-CE RELATED BRANCHES)
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
What is the output of the Java code snippet? B 1 main Thread-0 Compile Error Runtime Exception
What is the output of the Java code snippet? A 1 Sleep Join Sleep Compile Error Runtime Exception
class Wait extends Thread implements Runnable {
public static void main(String[] args) {
Wait run = new Wait();
Thread stop =new Thread(run);
run.start();
}
161 4 public void run( ){
System.out.print("Join ");
}
public void start( ){
System.out.print("Sleep ");
}
}
Write an application that creates and starts three threads. Each thread is 3
instantiated from the
same class. It executes a loop with 10 iterations. Each iteration displays
162 4 string "HELLO",
sleeps for 300 milliseconds. The application waits for all the threads to
complete & displays
the message "Good Bye..."
Write a program to create a child thread to print integer numbers 1 to 10 3
163 4
Write a program to create two threads, one thread will check whether 3
164 4 given number is prime or not and second thread will print prime numbers
between 0 to 100
L.J Institute of Engineering and Technology, Ahmedabad.
Computer Programming using Java-II PRACTICE BOOK (SEM-II-EVEN_2025-CE RELATED BRANCHES)
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
It's IPL 2023 time. In this process of IPL-23 there is a thread called Bowler. 5
This Bowler is going to create two threads with name – “Yorker” and
“googly”.
Write a java program to perform following task with each thread.
Task 1 : if the thread name is "yorker" then it should print "SIXER"
Task 2 : if the thread name is "googly" then it should print "WICKET"
Make sure first thread bowl must be yorker. When execution of first
165 4 thread is completed then and only then second bowl googly should work.
Hint: Here Bowler is a Thread Class. and It must have execution of SIXER
AND WICKET. Both yorker and googly must be from Bowler Thread Only.
Give name IPL to the class of Main method.
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
Write an application that read limit from user and executes two threads. 4
One thread displays total of first n even numbers & another thread
171 4 displays total of first n odd numbers. Create the threads by implementing
the Runnable interface
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
Problem Statement: Implement a Bank Account 5
UNIT 5- Collection-Part 1
Which of these class object can be used to form a dynamic array? B 1 Array ArrayList & Vector Both Only Vector Only ArrayList
180 5
What does get(int index) method define by List interface do ? C 1 stores an object at the delete the object at returns the object at returns the object at
181 5 specified index specified index specified index specified index and
modifies it’s value
What will be the output of the following code? C 1 Saha Rahul Compile time error Runtime error
import java.util.ArrayList;
class IPL{
public static void main(String args[])
{
ArrayList<String> gt1 = new ArrayList<String>();
182 5
gt1.addList(“Hardik”);
gt1.addList(“Saha”);
gt1.addList(“Rahul”);
gt1.remove(2);
System.out.println(gt1.get(1));
} }
What is the output of the following code? D 1 Compile time error [A, B, null] [A, B] [A, B, A, null]
import java.util.ArrayList;
import java.util.List;
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
What is the output of the following code? C 1 [A, B] [P, Q] [A, B, P, Q] [P, Q, A, B]
import java.util.ArrayList;
import java.util.List;
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
What is the output of the Java code snippet? B 1 [YES, NO] [YES, NO, 10] [10, YES, NO] [YES]
import java.util.*;
class New {
public static void main(String[] args) {
ArrayList a1 = new ArrayList(50);
a1.addAll(Arrays.asList("YES", "NO"));
189 5
ArrayList<Integer> a2 = new ArrayList<Integer>(a1);
a2.add(10);
System.out.print(a2);
}
}
190 5 In Java, the underlying data structure for the LinkedList class is? B 1 Singly linked list Doubly linked list Circular linked list None of these
191 5 LinkedList class implements? C 1 List interface Deque interface both List & Deque Vector & Stack
interface
What is the output? C 1 [100, 220, 50, 100, 50, [100, 220, 50, null] [100, 220, 50, null, 100, 50, None of these
import java.util.LinkedList; 100] 100, null]
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Integer> ll = new LinkedList<Integer>();
ll.add(100);
192 5 ll.add(220);
ll.add(50);
ll.add(null);
ll.add(100);
ll.add(50);
ll.add(100);
ll.add(null);
System.out.println(ll); } }
What is the output? C 1 [100, 220, 50, 100, 50] [999, 100, 220, 50, 888] [999, 100, 220, 50, 100, [999, 100, 220, 50, 100,
import java.util.LinkedList; 50, 888] 50, 888,null]
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
What is the output of following code snippet? C 1 [D, A, B, F, C, E] [D, A, B, C, F, E] [D, A, F, B, C, E] [D, A, B, C, E, F]
LinkedList<String> list = new LinkedList<String>();
list.add("A");
list.add("B");
list.add("C");
194 5
list.addFirst("D");
list.addLast("E");
list.add(2, "F");
System.out.println(list);
Which of the following methods is used to get the last element of a A 1 getLast() getLastNode() getLastElement() getFirst()
196 5
LinkedList in Java?
Collection is ______________ and Collections is ____________. A 1 Interface and class class and Interface In built framework & class and class
197 5
interface
What does the frequency() method of the Collections class do? A 1 Returns the number of Returns the number of Returns the number of None of the above
198 5 occurrences of an element occurrences of an element occurrences of an element
in a List in a Set in a Map
What is the output of the following code snippet? C 1 5 1 8 3
List<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(3);
199 5 list.add(8);
list.add(1);
Collections.sort(list);
System.out.println(Collections.max(list));
L.J Institute of Engineering and Technology, Ahmedabad.
Computer Programming using Java-II PRACTICE BOOK (SEM-II-EVEN_2025-CE RELATED BRANCHES)
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
What will be the output of the following code? C 1 55 5 0707 50101505
import java.util.*;
class xyz
{
public static void main(String[] args)
{
ArrayList a = new ArrayList(Arrays.asList(5 , 0 , 10 , 7 , 15 , 0 , 5 , 7));
200 5 Iterator i = a.iterator();
while(i.hasNext())
{
if( ( int ) i.next() % 5 == 0)
System.out.print( i.next());
}
}}
Which of the following is a valid way to reverse the order of the elements C 1 list.reverse() reversed(list) Collections.reverse(list) collections.reverse(list)
201 5
of a List using the Collections class?
Which of the following is a valid way to find the smallest element in a List A 1 Collections.min(list) list.min() Collections.minimum(list) list.minimum()
202 5
using the Collections class?
Which of the following is a valid way to find the frequency of an element C 1 Collections.count(list, list.count(element) Collections.frequency(list, list.frequency(element)
203 5
in a List using the Collections class? element) element)
Which of these methods is used to add elements in vector at specific A 1 add() set() AddElement() addElement()
204 5
location?
What is the correct syntax to declare a vector in Java? A 1 Vector vec = new Vector(); Vector vec[]; Vector vec = {1, 2, 3}; None of the above.
205 5
Which method is used to add an element to the end of a vector? A 1 add() insert() push() append()
206 5
207 5 What is the default capacity of a vector in Java? B 1 50 10 100 20
What is the correct syntax to create a vector of Strings in Java? A 1 Vector vec = new vector<String> vec = new vector vec = new Vector(); None of the above.
208 5
Vector<String>(); Vector();
Which of the following methods is used to insert an element at a specific B 1 add() set() addAll() setAll()
209 5
index in a vector?
Which is not correct way to get the last element of a vector in Java? A 1 vec.get(0) vec.get(vec.size() - 1) vec.lastElement() vec.elementAt(vec.size() -
210 5
1)
What is the output of the following code snippet? B 1 1 2 3 4
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
What is the output of the following code ? D 1 [3, 2, 6] [3, 2, 6, 6] [3, 2, 6, 8] [3, 2, 8, 6]
import java.util.*;
class vector
{
public static void main(String args[])
{
212 5 Vector obj = new Vector();
obj.addElement(new Integer(3));
obj.addElement(new Integer(2));
obj.addElement(new Integer(6));
obj.insertElementAt(new Integer(8), 2);
System.out.println(obj);
} }
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
What is the output of the following code snippet? B 1 10 20 14 13
Vector<Integer> vec = new Vector<Integer>();
vec.add(1);
vec.add(2);
vec.add(3);
vec.add(1);
vec.add(2);
216 5 vec.add(1);
vec.add(2);
vec.add(1);
vec.add(2);
vec.add(1);
vec.add(2);
vec.add(1);
vec.add(2); System.out.println(vec.capacity());
What will be the output of the following code? C 1 240 3920 280 null40
import java.util.*;
class xyz {
public static void main(String[] args) {
Vector s = new Vector ();
for(int i =0 ; i < 39 ; i++)
{
217 5 s.add(i);
}
s.add(2);
s.add(null);
System.out.print(s.get(39));
System.out.println(s.capacity());
}
}
What is the output of the Java code snippet? D 1 17[2, 0, 2, null, Easy] 17[2, 0, LJ, 2, null, Easy] Compile Error Runtime Exception
import java.util.*;
class Test {
public static void main(String[] args) {
Vector v = new Vector<>(Arrays.asList(2, "LJ", 'h', 2, null));
v.add("Easy");
218 5
v.add(1, 0);
v.remove('h');
System.out.print(v.capacity() + v.size());
System.out.print(v);
}
}
219 5 Stack class is designed for ? A 1 FILO Order FIFO ORDER LILO ORDER None of the above
L.J Institute of Engineering and Technology, Ahmedabad.
Computer Programming using Java-II PRACTICE BOOK (SEM-II-EVEN_2025-CE RELATED BRANCHES)
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
What will be the output of the following code? D 1 Kotlin Java Kotlin Java null Compile time error Exception at run time
import java.util.Stack;
What is the output of the following? C 1 [50] [100, 50, 200, 220, 50] [100, 50, 220, 50] [220, 50]
import java.util.Stack;
public class Test {
public static void main(String[] args) {
Stack<Integer> st = new Stack<Integer>();
st.add(100);
st.push(50);
221 5 st.add(200);
st.pop();
st.add(220);
st.push(50);
System.out.println(st);
} }
What is the output of the following code? A 1 true false false true true true false false
import java.util.Stack;
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
import java.util.*; A 1 [3, 5] [3, 5, 2] [3, 5, 2, null] [3, 2, 5]
class stack
{
public static void main(String args[])
{
Stack obj = new Stack();
224 5
obj.push(new Integer(3));
obj.push(new Integer(2));
obj.pop();
obj.push(new Integer(5));
System.out.println(obj);
} }
What is the output of the following code? A 1 02 01 compile time error run time error
import java.util.Stack;
class TestDemo2{
public static void main(String[] args) {
Stack s1 = new Stack();
System.out.print(s1.size()+" ");
s1.push("Hello");
225 5 s1.push(20.23);
s1.push("JJS");
s1.peek();
s1.peek();
s1.peek();
s1.pop();
System.out.println(s1.size());
} }
What will be the output of the following code? D 1 10 31 13 -1 -1
import java.util.*;
class xyz {
public static void main(String[] args) {
Stack<String> st = new Stack<String>();
226 5 st.push("Java");
st.push("Kotlin");
st.push("Python");
System.out.println(st.search("java") + " " + st.search("SQL"));
}
}
Write a Java program to create a new array list, add some colors (string) 3
228 5 and print out the collection
L.J Institute of Engineering and Technology, Ahmedabad.
Computer Programming using Java-II PRACTICE BOOK (SEM-II-EVEN_2025-CE RELATED BRANCHES)
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
Write a Java program that creates an ArrayList<Student> to store a name 3
and marks of 3 students. Then sort the ArrayList by student marks in
descending order. Then print arraylist using iterator. Add these details of
3 Students in arraylist in given sequence:
Devarsh (18.5 marks), Viraj (20.5 marks) and Nidhi (17.5 marks)
229 5
Desired Output:
Name=Viraj, Marks=20.5
Name=Devarsh, Marks=18.5
Name=Nidhi, Marks=17.5
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
Write a Java program that creates an ArrayList to store a list of students 6
and their grades. The program should provide the following functionality:
Add a student: The program should prompt the user for a student name
and their grade, and add the student to the ArrayList.
Remove a student: The program should prompt the user for a student
name and remove the corresponding entry from the ArrayList.
Search for a student: The program should prompt the user for a student
name and output their corresponding grade. If the student name is not
found in the ArrayList, the program should output an error message.
Sort students by name: The program should sort the ArrayList by student
name in ascending order.
Sort students by grade: The program should sort the ArrayList by student
grade in descending order.
To accomplish these tasks, you can use the following Collections class
methods:
235 5 addAll(collection, elements): Adds all elements from one collection to
another. sort(list): Sorts the elements in a list in natural order.
sort(list, comparator): Sorts the elements in a list using a custom
comparator. reverse(list): Reverses the order of the elements in a list.
You can also use the following ArrayList methods:
add(element): Adds an element to the end of the ArrayList.
remove(element): Removes the first occurrence of an element from the
ArrayList. get(index): Returns the element at the specified index in the
ArrayList. size(): Returns the number of elements in the ArrayList.
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
Write a Java program to get the first and last occurrence of the specified 3
238 5 elements in a linked list.
Write a Java program to clone an linked list to another linked list. 3
239 5
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
Write a Java program to sort a stack of Student objects by their GPA. You 5
need to implement a function that takes a stack of Student objects as
input and returns a new stack with the Students sorted in descending
order of their GPA.
245 5
To solve this problem, you can create a Student class with attributes such
as name, id, and GPA. You can then create a stack of Student objects and
implement a function to sort the stack by their GPA.
Use a Stack data structure to store the correct answers to all 5 MCQs.
Each answer should be represented as a character in the stack. This stack
will serve as the reference for evaluating the participants' answers. The
correct answer sequence is as follows: A, B , A , D , C.
246 4,5 After all participants have submitted their answers, calculate and display 9
each participant's score. The score should be calculated by comparing
each participant's submitted answers (ArrayList) with the correct answers
L.J Institute of Engineering and Technology, Ahmedabad.
Computer Programming using Java-II PRACTICE BOOK (SEM-II-EVEN_2025-CE RELATED BRANCHES)
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
each participant's submitted answers (ArrayList) with the correct answers
(Stack). Award 1 point for each correct answer and calculate the total
score for each participant out of 5.
Finally, declare the winner based on the participant with the highest
score. In case of tie, younger participant will be declared as winner.
Filename: ConsProd.java
You are tasked with implementing a multi-threaded program in Java that
simulates the Consumer Producer problem. In this problem, you have a
shared buffer between a consumer and a producer. The producer
generates data and puts it into the buffer, while the consumer retrieves
data from the buffer.
Note : This PRACTICE BOOK is only for reference purpose. LJU Test question paper may not be completely set from this PRACTICE BOOK.
Unit MCQ
Sr No Question_Text Marks Option A Option B Option C Option D
Number Answer
Producer: Create a Producer thread class that generates data items and
puts them into the buffer. It repeatedly produces (total 5) values and
sleeps for 1 second between each production. The producer should wait if
the buffer is full.
Consumer: Create a Consumer thread class that retrieves data items from
the buffer. It repeatedly consumes the produced value (total 5) and sleeps
for 1 second between each consumption.The consumer should wait if the
buffer is empty.
Main Class (ConsProd): This is the entry point of the program. Create an
247 4,5 9
object of the SharedBuffer class and two threads - one for the producer
and one for the consumer. Then start both threads to achieve goal.
Sample output: (assume above mentioned delay between each
production and each consumption)
Produced: 1
Consumed: 1
Produced: 2
Consumed: 2
Produced: 3
Consumed: 3
Produced: 4
Consumed: 4
Produced: 5
Consumed: 5