DSA MCQ
DSA MCQ
5. What is an algorithm?
a) A programming language
b) A flowchart
c) A set of well-defined steps to solve a problem
d) A computer program
✅ Answer: c) A set of well-defined steps to solve a problem
ARRAY:-
a) 4
b) 5
c) 6
d) Compiler error
✅ Answer: b) 5
a) 0
b) 2
c) Compilation error
d) Runtime error
✅ Answer: a) 0
a) 1
b) 4
c) Compilation error
d) Runtime error
✅ Answer: b) 4
A) 10
B) 20
C) 30
D) 40
Answer: C) 30
A) Compilation error
B) Runtime error
C) 0
D) Garbage value
Answer: C) 0
A) 4
B) 5
C) Compilation error
D) Runtime error
Answer: B) 5
24. What will be the output of the following code?
A) 10
B) 20
C) Compilation error
D) Runtime error
Answer: B) 20
A) 1
B) 2
C) 3
D) 4
Answer: D) 4
A) 0
B) false
C) true
D) null
Answer: B) false
A) 1
B) 2
C) 3
D) 4
Answer: D) 4
A) Compilation error
B) Runtime error
C) 0
D) Garbage value
Answer: C) 0
A) 10
B) 40
C) Compilation error
D) Runtime error
A) Compilation error
B) 40
C) Runtime error
D) 30
A) int[5] arr;
B) int arr[5];
C) int[] arr = new int[5];
D) int arr = new int[5];
A) Alice
B) Bob
C) Charlie
D) Compilation error
Answer: B) Bob
A) 0
B) null
C) Garbage value
D) Compilation error
Answer: A) 0
A) 10 20 30
B) 10 20
C) 30 20 10
D) Compilation error
Answer: A) 10 20 30
A) Compilation error
B) Runtime error (ArrayIndexOutOfBoundsException)
C) The value will be 0
D) It will return null
A) 3
B) 4
C) 5
D) Compilation error
Answer: B) 4
A) O(1)
B) O(n)
C) O(log n)
D) O(n^2)
Answer: A) O(1)
A) true
B) false
C) Compilation error
D) Runtime error
Answer: A) true
String
A) 4
B) 5
C) 6
D) Compilation error
Answer: D
A) J
B) a
C) v
D) Compilation error
Answer: C) v
A) true
B) false
C) Compilation error
D) Runtime error
Answer: A) true
A) true
B) false
C) Compilation error
D) Runtime error
Answer: B) false
A) Hello
B) Hello World
C) World
D) Compilation error
A) hello
B) HELLO
C) HeLLo
D) Compilation error
Answer: B) HELLO
Answer: B) "Java"
A) Ja
B) av
C) va
D) Compilation error
Answer: B) av
A) Jovo
B) Jova
C) Jovoa
D) Compilation error
Answer: A) Jovo
A) true
B) false
C) Compilation error
D) Runtime error
Answer: A) true
A) 1
B) 2
C) 3
D) -1
Answer: A) 1
12. What will be the output of the following code?
A) true
B) false
C) Compilation error
D) Runtime error
Answer: A) true
A) String s = "";
B) String s = new String();
C) Both A and B
D) None of the above
A) true
B) false
C) Compilation error
D) Runtime error
Answer: A) true
A) true
B) false
C) Compilation error
D) Runtime error
Answer: B) false
A) Java
B) Programming
C) Java Programming
D) Compilation error
Answer: A) Java
A) Hello
B) nullHello
C) Compilation error
D) Runtime error
A) Java
B) Java Programming
C) Compilation error
D) Runtime error
A) hello
B) hewwo
C) hwwwo
D) Compilation error
Answer: B) hewwo
a) 3
b) 4
c) 5
d) Compiler error
✅ Answer: b) 4
String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);
a) true
b) false
c) Compilation error
d) Runtime error
✅ Answer: a) true
Linked List.
A) 10
B) 20
C) 30
D) Compilation error
Answer: B) 20
2. What will be the output of the following code?
public class Main {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head = head.next;
System.out.println(head.data);
}
}
A) 10
B) 20
C) null
D) Compilation error
Answer: B) 20
Answer: B) null
A) O(1)
B) O(n)
C) O(log n)
D) O(n²)
Answer: A) O(1)
A) O(1)
B) O(n)
C) O(log n)
D) O(n²)
Answer: B) O(n)
Answer: C) 15
A) 0
B) null
C) false
D) Garbage value
Answer: B) null
A) O(1)
B) O(n)
C) O(log n)
D) O(n²)
Answer: B) O(n)
A) 1
B) null
C) Compilation error
D) Infinite loop
Answer: A) 1
A) O(1)
B) O(n)
C) O(log n)
D) O(n²)
Answer: B) O(n)
A) O(1)
B) O(n)
C) O(log n)
D) O(n²)
Answer: A) O(1)
13. What will happen if we access an index greater than the size
of the LinkedList?
A) Returns null
B) Throws an IndexOutOfBoundsException
C) Prints 0
D) Compiles but runs infinitely
A) 5
B) 10
C) null
D) Compilation error
Answer: C) null
A) O(1)
B) O(n)
C) O(log n)
D) O(n²)
Answer: B) O(n)
A) O(1)
B) O(n)
C) O(log n)
D) O(n²)
Answer: B) O(n)
A) true
B) false
C) Compilation error
D) Runtime error
Answer: A) true
A) O(1)
B) O(n)
C) O(log n)
D) O(n²)
Answer: B) O(n)
31. What is the main difference between a Singly Linked List and
a Doubly Linked List?
a) Singly Linked List stores only one element
b) Doubly Linked List has references to both next and previous
nodes
c) Singly Linked List cannot store duplicate values
d) None of the above
✅ Answer: b) Doubly Linked List has references to both next and
previous nodes
35. What is the time complexity for inserting a node at the tail
of a Circular Linked List?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
✅ Answer:
Recursion:-
A) 1 2 3 4 5
B) 5 4 3 2 1
C) 5 4 3 2
D) Infinite loop
Answer: B) 5 4 3 2 1
A) 3 2 1
B) 1 2 3
C) 3 2 1 0
D) Compilation error
Answer: B) 1 2 3
A) if (n == 0) return 0;
B) if (n == 1) return 1;
C) return n + sum(n - 1);
D) No base case
Answer: B) if (n == 1) return 1;
A) 24
B) 10
C) 4
D) Compilation error
Answer: A) 24
A) 5
B) 8
C) 10
D) 12
Answer: B) 8
A) O(n)
B) O(2^n)
C) O(n^2)
D) O(log n)
Answer: B) O(2^n)
A) 10 5 2 1
B) 10 5 2 1 0
C) 10 5 2
D) 10 2 1
Answer: A) 10 5 2 1
A) Runs indefinitely
B) Throws an exception
C) Runs but produces no output
D) Compiles but does nothing
A) 3 2 1
B) 1 2 1 3 1 2 1
C) 1 2 3 2 1
D) Compilation error
Answer: B) 1 2 1 3 1 2 1
A) 4 3 2 1
B) 1 2 3 4
C) 4 3 2 1 0
D) Compilation error
Answer: A) 4 3 2 1
A) 8
B) 16
C) 4
D) 32
Answer: A) 8
A) 3 2 1 1 2 3
B) 3 2 1
C) 1 2 3
D) 3 2 1 2 3
Answer: A) 3 2 1 1 2 3
A) O(1)
B) O(n)
C) O(log n)
D) O(n^2)
Answer: B) O(n)
A) Bubble Sort
B) Quick Sort
C) Selection Sort
D) Insertion Sort
A) 8
B) 6
C) 9
D) 12
Answer: A) 8
a) Yes
b) No
✅ Answer: a) Yes
a) 3 2 1 0
b) 3 2 1
c) 1 2 3
d) Compilation error
✅ Answer: b) 3 2 1
a) Yes
b) No
✅ Answer: a) Yes
a) 1 2 3 4 5
b) 1 2 3 4 5 6
c) Infinite recursion
d) Compilation error
✅ Answer: a) 1 2 3 4 5
a) 1 2 3
b) 3 2 1
c) 0 1 2 3
d) Infinite loop
✅ Answer: a) 1 2 3