STS_java
STS_java
Options:
a) int
b) decimal
c) String
d) Object
Options:
a) 8 bits
b) 16 bits
c) 32 bits
d) 64 bits
Options:
a) decimal
b) boolean
c) char
d) long
5. What is the maximum value that can be stored in a byte variable in Java?
Options:
a) 128
b) 127
c) 256
d) 255
Correct Answer: b) 127
Options:
a) 32 bits
b) 64 bits
c) 128 bits
d) 256 bits
7. In Java, what is the range of values that can be stored in a short variable?
Options:
a) -128 to 127
b) -32768 to 32768
c) -32,768 to 32,767
d) -2,147,483,648 to 2,147,483,647
Options:
a) float
b) int
c) double
d) byte
Options:
a) boolean
b) int
c) char
d) byte
Options:
a) 1 bit
b) 8 bits
c) 16 bits
d) 32 bits
Options:
a) Reading user input
b) Writing text to the console
c) Opening a file
d) Performing arithmetic operations
2. Which class provides methods for reading input from the user in Java?
Options:
a) Scanner
b) System
c) InputReader
d) BufferedReader
3. What is the method used to read a string from the user using Scanner?
Options:
a) readLine()
b) readString()
c) nextLine()
d) getString()
Options:
a) \n
b) \r
c) \t
d) \\
Correct Answer: a) \n
Options:
a) Represents a new line character
b) Represents a tab character
c) Represents the percentage symbol
d) Represents a null character
Options:
a) %
b) &
c) /
d) *
Correct Answer: a) %
Options:
a) Standard output stream
b) Standard error stream
c) Standard input stream
d) Standard logging stream
java
Copy code
java
Copy code
Options:
a) Value: 12.3
b) Value: 12.345
c) Value: 12.34
d) Value: 12.35
1. Which control statement is used to execute a block of code repeatedly based on a given
condition?
Options:
a) if statement
b) for loop
c) while loop
d) do-while loop
2. The switch statement in Java can only be used with which data types?
Options:
a) int and char
b) int and float
c) char and float
d) int and double
java
Copy code
int x = 5;
if (x > 3) {
} else {
Options:
a) x is greater than 3
b) x is not greater than 3
c) x
d) 5
4. Which control statement is used to exit a loop prematurely when a specific condition is met?
Options:
a) break statement
b) continue statement
c) return statement
d) exit statement
Options:
a) The loop will not execute.
b) An error will occur during compilation.
c) The loop will run infinitely.
d) The loop will execute only once.
Options:
a) switch(condition) { }
b) switch { }
c) case(condition) { }
d) case { }
Options:
a) for loop
b) while loop
c) do-while loop
d) until loop
Options:
a) To handle exceptions
b) To provide an alternative code block if the condition is false
c) To stop the execution of the program
d) To restart the execution of the program
java
Copy code
int x = 10;
if (x % 2 == 0) {
if (x > 5) {
} else {
} else {
System.out.println("x is odd");
Options:
a) x is even and greater than 5
b) x is even and not greater than 5
c) x is odd
d) No output will be printed
Correct Answer: a) x is even and greater than 5
Options:
a) To restart the loop
b) To skip the current iteration and move to the next one
c) To force the immediate termination of the loop
d) To return a value from the loop
1. What is an algorithm?
Options:
a) A set of rules to decorate code
b) A step-by-step procedure to solve a problem
c) A programming language
d) A type of data structure
Options:
a) To make code more complicated
b) To optimize resource usage and reduce execution time
c) To confuse other programmers
d) To avoid using data structures
3. What characteristic ensures that an algorithm will eventually stop after a finite number of steps?
Options:
a) Input
b) Output
c) Definiteness
d) Finiteness
4. Which characteristic ensures that the algorithm's steps can be executed using basic operations
or actions?
Options:
a) Effectiveness
b) Correctness
c) Determinism
d) Feasibility
Options:
a) Memory complexity
b) Syntax correctness
c) Runtime error handling
d) Proper indentation
6. Which type of algorithm aims to find a global optimum by making locally optimal choices?
Options:
a) Sorting Algorithms
b) Searching Algorithms
c) Greedy Algorithms
d) Backtracking Algorithms
7. Which algorithmic technique breaks a problem into smaller sub-problems and solves them
independently?
Options:
a) Dynamic Programming Algorithms
b) Divide and Conquer Algorithms
c) Backtracking Algorithms
d) Greedy Algorithms
Options:
a) Input
b) Output
c) Definiteness
d) Finiteness
Options:
a) Sorting Algorithms
b) Graph Algorithms
c) Dynamic Programming Algorithms
d) Searching Algorithms
Options:
a) To confuse other programmers
b) To make code more complicated
c) To improve code readability and consistency
d) To avoid using data structures
Options:
a) The number of iterations
b) The number of function calls
c) The number of conditional statements
d) The number of variables used
2. To verify whether a function grows faster or slower than the other function, we have some
asymptotic or mathematical notations, which is_________.
Options:
a) Big Omega Ω(f)
b) Big Theta θ(f)
c) Big Oh O(f)
d) All of the above
3. What does it mean when we say that an algorithm X is asymptotically more efficient than Y?
Options:
a) X will always be a better choice for small inputs
b) Y will always be a better choice for small inputs
c) X will always be a better choice for large inputs
d) X will always be a better choice for all inputs
Correct Answer: c) X will always be a better choice for large inputs
Options:
a) Quicksort
b) Merge sort
c) Bubble sort
d) Heap sort
Options:
a) describes limiting behavior of the function
b) characterizes a function based on growth of function
c) upper bound on growth rate of the function
d) all of the mentioned
6. Which of the following is the most efficient algorithm in terms of time complexity?
Options:
a) O(n)
b) O(n^2)
c) O(n log n)
d) O(2^n)
7. What is the time complexity of an algorithm that performs n/2 operations in the worst case
scenario?
Options:
a) O(n)
b) O(log n)
c) O(n log n)
d) O(n^2)
8. Ο(log n) is?
Options:
a) constant asymptotic notations
b) logarithmic asymptotic notations
c) polynomial asymptotic notations
d) quadratic asymptotic notations
Copy code
int a = 0;
a = a + i + j;
Options:
a) O(N)
b) O(Nlog(N))
c) O(N * Sqrt(N))
d) O(NN)
10. What is the space complexity of an algorithm that uses an array of size n to store the input, and
another array of size m to store the output?
Options:
a) O(1)
b) O(n)
c) O(m)
d) O(n + m)
1. Which of the following is an optimization used in the Sieve of Eratosthenes algorithm to reduce
memory usage?
Options:
A. Using a linked list instead of an array
B. Marking only odd numbers as prime
C. Using a heap instead of a stack
D. None of the above
Options:
A. O(1)
B. O(n)
C. O(n log n)
D. O(n^2)
Options:
A. Create a boolean array of size n+1
B. Mark all multiples of 2 as composite
C. Mark all multiples of 3 as composite
D. Find the greatest common divisor of each number
Options:
A. The number is divisible by 2
B. The number is divisible by 3
C. The number is divisible by any prime less than its square root
D. The number is divisible by any prime less than itself
Correct Answer: C. The number is divisible by any prime less than its square root
5. How many prime numbers less than or equal to 100 can be found using the Sieve of
Eratosthenes algorithm?
Options:
A. 24
B. 25
C. 26
D. 13
Correct Answer: B. 25
6. Which of the following optimizations can be applied to Simple Sieve to reduce its memory
usage?
Options:
A. Storing only odd numbers in the sieve
B. Using bit packing to store the boolean flags
C. Both A and B
D. None of the above
Options:
A. Requires less memory
B. Requires more memory
C. Takes less time to execute
D. Takes more time to execute
8. Which of the following data structures can be used to implement Simple Sieve efficiently?
Options:
A. Array
B. Linked list
C. Stack
D. Queue
Options:
A. Segmented Sieve
B. Incremental Sieve
C. Both A and B
D. None of the above
10. How many prime numbers are there between 100 and 200?
Options:
A. 23
B. 21
C. 10
D. 12
Correct Answer: A. 23
1. What is the main advantage of the segmented sieve algorithm over the traditional sieve of
Eratosthenes algorithm?
Options:
a) It uses less memory.
b) It is faster for small ranges of numbers.
c) It is easier to implement.
d) It can handle larger ranges of numbers.
Options:
a) It only finds prime numbers.
b) It finds both prime and composite numbers.
c) It only finds composite numbers.
d) It does not find any numbers.
3. What is the output of the incremental sieve when n=20 is given as input?
Options:
a) 2 3 5 7 11 13 17
b) 2 3 5 7 11 13 17 19 20
c) 2 3 5 7 11 13 17 19
d) 2 3 5 7 12 13 17 19
Correct Answer: c) 2 3 5 7 11 13 17 19
python
Copy code
f():
ans = 0
for i = 1 to n:
for j = i; j <= n; j += i:
ans += 1
print(ans)
Options:
a) O(log n)
b) O(n)
c) O(n log n)
d) O(n * n)
Options:
a) O(n)
b) O(n log n)
c) O(n log log n)
d) O(sqrt(n))
Options:
a) It requires a large amount of memory.
b) It is slower than the incremental sieve.
c) It only works for small ranges.
d) It can only find prime numbers.
8. What is the formula for calculating the nth prime number using the Incremental Sieve
algorithm?
Options:
a) n * (n - 1) + 2
b) n * n - n + 1
c) n * n + n + 1
d) n * n + 2
Correct Answer: d) n * n + 2
9. Which algorithm is best suited for finding all prime numbers in a small range?
Options:
a) Segmented sieve
b) Incremental sieve
c) Both algorithms are equally suited for this task.
d) Neither algorithm is suited for this task.
10. What is the advantage of using the Segmented Sieve algorithm over the trial division method
for prime number generation?
Options:
a) The Segmented Sieve algorithm is faster
b) The Segmented Sieve algorithm is simpler to implement
c) The Segmented Sieve algorithm can be parallelized
d) The Segmented Sieve algorithm can generate prime numbers up to a larger limit
Options:
A) A function that counts the number of divisors of an integer.
B) A function that counts the number of prime factors of an integer.
C) A function that counts the number of integers less than a given integer n that are
relatively prime to n.
D) A function that counts the number of integers less than a given integer n that are divisible
by n.
Correct Answer: C) A function that counts the number of integers less than a given integer n
that are relatively prime to n.
2. What is the relationship between the values of ϕ(n) and ϕ(p^k) for prime p and positive integer
k?
Options:
A) ϕ(n) = p^k - 1
B) ϕ(n) = p^(k-1)
C) ϕ(n) = p^k
D) ϕ(n) = (p-1)*p^(k-1)
Options:
A) ϕ(nm) = ϕ(n) + ϕ(m)
B) ϕ(nm) = ϕ(n)ϕ(m)
C) ϕ(nm) = ϕ(n) - ϕ(m)
D) None of the above
Options:
A) 98
B) 90
C) 108
D) 120
Correct Answer: B) 90
Options:
A) 0
B) 1
C) 2
D) 3
Correct Answer: C) 2
Options:
A) 0
B) 1
C) 2
D) 4
Correct Answer: D) 4
Options:
A) 1
B) 2
C) 6
D) 4
Correct Answer: D) 4
Options:
A) 0
B) 1
C) 2
D) 4
Correct Answer: D) 4
Options:
A) φ(n) is always even for any positive integer n.
B) φ(n) is equal to the number of divisors of n.
C) φ(p) = p-1 for any prime number p.
D) φ(n) is always greater than n for any positive integer n.
10. What is the value of Euler's Totient Function for the number 2000?
Options:
A) 789
B) 880
C) 800
D) 670
Options:
A) A number that is divisible by 3
B) A number that reads the same when rotated 180 degrees
C) A number that is a multiple of 5
D) A number that contains only even digits
Correct Answer: B) A number that reads the same when rotated 180 degrees
Options:
A) 818
B) 1881
C) 969
D) 666
Options:
A) They are only found in base 10
B) They are always palindromic
C) They can be used in cryptography to encode messages
D) They are always even
5. A number that reads the same when rotated 180 degrees. What is the strobogrammatic number
that is the largest prime number?
Options:
A) 11
B) 101
C) 181
D) There is no strobogrammatic number that is prime
Options:
A) 818
B) 1881
C) 969
D) 666
Correct Answer: D) 666
Options:
A) They are only found in base 10
B) They are always palindromic
C) They can be used in cryptography to encode messages
D) They are always even
Options:
A) They are always prime
B) They are always even
C) They are always palindromic
D) They are always odd
Options:
A) They provide a higher level of security.
B) They enable faster encryption and decryption processes.
C) They are resistant to data breaches and hacking.
D) They improve the efficiency of key generation.
Options:
A) They lead to inaccurate results in division operations.
B) They require specialized algorithms for addition and subtraction.
C) They cause a slowdown in multiplication and exponentiation.
D) They are incompatible with decimal and fractional numbers.
Correct Answer: B) They require specialized algorithms for addition and subtraction.
2. Which of the following statements is true regarding the Chinese Remainder Theorem?
Options:
A) It can only be applied to solve systems of congruences with prime moduli.
B) It can be applied to solve systems of congruences with coprime moduli.
C) It can be applied to solve systems of congruences with any moduli.
D) It can only be applied to solve systems of congruences with integer moduli.
Correct Answer: B) It can be applied to solve systems of congruences with coprime moduli.
java
Copy code
int num = 8;
int divisor = 3;
Options:
A) 3 3
B) 2 2
C) 1 2
D) 3 4
Correct Answer: C) 1 2
Options:
A) 114
B) 119
C) 123
D) 456
Options:
A) It is a theorem in calculus that relates to finding the remainder of a polynomial function
after dividing it by another polynomial function.
B) It is a theorem in number theory that states that for any integer n and any integer a, there
exists a unique integer q and r such that n = aq + r, where r is the remainder.
C) It is a theorem in algebra that states that if a polynomial f(x) is divided by x-a, then the
remainder is equal to f(a).
D) It is a theorem in statistics that relates to finding the remainder of a set of data after
dividing it by another set of data.
Correct Answer: B) It is a theorem in number theory that states that for any integer n and
any integer a, there exists a unique integer q and r such that n = aq + r, where r is the
remainder.
Options:
A) Number theory
B) Geometry
C) Algebra
D) Calculus
Options:
A) It requires advanced knowledge of complex numbers.
B) It can only be applied to linear equations.
C) It has limited applicability to certain types of problems.
D) It is computationally intensive and time-consuming.
9. x ≡ 1 (mod 5), x ≡ 3 (mod 7). What is the smallest positive integer that satisfies these
congruences using the Chinese Remainder Theorem?
Options:
A) 17
B) 11
C) 10
D) 13
Correct Answer: D) 13
Options:
A) Generating random numbers.
B) Encrypting messages.
C) Decrypting messages.
D) Hashing algorithms.
Options:
A) Square
B) L-shape
C) T-shape
D) Hourglass
Options:
A) 5
B) 6
C) 7
D) 8
Correct Answer: C) 7
Correct Answer: B) 24
4. Which of the following best describes the process of finding an hourglass sum in a matrix?
Options:
A) Calculate the sum of all elements in the matrix.
B) Calculate the sum of elements in each possible hourglass shape.
C) Calculate the sum of elements in each row.
D) Calculate the sum of elements in each column.
Correct Answer: B) Calculate the sum of elements in each possible hourglass shape.
5. What is the time complexity of finding the maximum hourglass sum in a 6x6 matrix?
Options:
A) O(1)
B) O(n)
C) O(n^2)
D) O(n^3)
6. Which of the following loops is essential in the implementation of finding an hourglass sum?
Options:
A) A single loop iterating over all elements.
B) Two nested loops iterating over rows and columns.
C) Three nested loops iterating over rows, columns, and diagonals.
D) A loop iterating only over diagonals.
Correct Answer: B) Two nested loops iterating over rows and columns.
7. What should be initialized before iterating through the matrix to find the maximum hourglass
sum?
Options:
A) Minimum possible integer value
B) Maximum possible integer value
C) Zero
D) Matrix size
Correct Answer: A) Minimum possible integer value
8. Which of the following Java methods is most suitable for storing hourglass sums while iterating
through the matrix?
Options:
A) ArrayList
B) Linked List
C) Priority Queue
D) int variable
9. Which part of the hourglass is typically ignored when calculating its sum?
Options:
A) Top row
B) Bottom row
C) Center element
D) None
Options:
A) By summing all hourglass sums.
B) By finding the minimum hourglass sum.
C) By comparing each hourglass sum with the current maximum.
D) By counting the total number of hourglasses.
Correct Answer: C) By comparing each hourglass sum with the current maximum.
1. What is the time complexity of the max() method in a Priority Queue in Java?
Options:
A) O(1)
B) O(log N)
C) O(N)
D) O(N log N)
2. In a concurrent environment, which of the following Java classes is best suited for maintaining a
maximum value with safe thread operations?
Options:
A) ConcurrentSkipListMap
B) ConcurrentHashMap
C) SynchronizedList
D) CopyOnWriteArrayList
3. If you have a TreeMap in Java and you want to find the maximum key, which method would you
use?
Options:
A) lastKey()
B) floorKey()
C) higherKey()
D) firstKey()
4. What is the primary reason to use a TreeSet when needing to maintain a maximum equilibrium
in a sorted collection?
Options:
A) It maintains natural ordering and does not allow duplicates.
B) It is synchronized and thread-safe.
C) It offers constant-time performance for basic operations.
D) It allows null values for maximum flexibility.
Correct Answer: A) It maintains natural ordering and does not allow duplicates.
5. How does the PriorityQueue class in Java determine the maximum element in a priority queue?
Options:
A) By using a max heap internally.
B) By using a min heap internally.
C) By using a balanced binary tree.
D) By sorting elements at runtime.
Options:
A) Use a balanced binary search tree.
B) Use a max-heap data structure.
C) Use an ArrayList with sorting after each insertion.
D) Use a HashMap to track maximum values.
8. When using Collections.max() method, what is the time complexity if the collection is a List?
Options:
A) O(1)
B) O(log N)
C) O(N)
D) O(N log N)
9. In Java, which of the following classes implements the Navigable Map interface and supports
operations for retrieving the maximum key?
Options:
A) HashMap
B) TreeMap
C) LinkedHashMap
D) ConcurrentHashMap
10. Given the following code snippet, what is the expected output if the PriorityQueue is used for
storing integers?
java
Copy code
pq.add(10);
pq.add(20);
pq.add(30);
System.out.println(pq.peek());
Options:
A) 10
B) 20
C) 30
D) 40
Correct Answer: C) 30
1. Which algorithm has the best time complexity for finding all leaders in an array?
Options:
A) O(n log n) using sorting
B) O(n) using a single traversal
C) O(n^2) using nested loops
D) O(n^2) using a priority queue
2. Given an array A of size n, what is the maximum number of leaders possible in the array?
Options:
A) 1
B) n-1
C) n
D) n/2
3. In an array with all elements being the same, how many leaders are there?
Options:
A) 0
B) 1
C) n-2
D) n
Correct Answer: B) 1
4. Consider the array A = [2, 4, 3, 6, 5, 8]. Which of the following elements is a leader?
Options:
A) 2
B) 4
C) 6
D) 8
Correct Answer: D) 8
5. If an array contains negative numbers only, what is true about the leader of the array?
Options:
A) The smallest negative number is always a leader.
B) The largest negative number is always a leader.
C) No negative number can be a leader.
D) All negative numbers are leaders.
6. Which of the following operations can help in finding leaders efficiently in a large array?
Options:
A) Precomputing the maximum values for suffixes
B) Sorting the array and then scanning
C) Using a hash map to store all elements
D) Computing all possible subarrays
7. Given an array A = [1, 2, 3, 4, 5, 6], which of the following statements is true about leaders in
this array?
Options:
A) All elements are leaders.
B) Only the last element is a leader.
C) The first element is a leader.
D) No elements are leaders.
8. For an array A of length n, what is the space complexity of the optimal solution for finding all
leaders?
Options:
A) O(n)
B) O(log n)
C) O(1)
D) O(n^2)
9. Which data structure can be utilized to efficiently keep track of the maximum elements seen so
far while traversing an array from right to left?
Options:
A) Stack
B) Queue
C) Linked List
D) Binary Search Tree
10. In an array A where A = [5, 7, 3, 7, 6, 8], which of the following pairs of elements could be
leaders if we choose the array A to be a modified version where some elements are shifted?
Options:
A) 5 and 8
B) 3 and 6
C) 7 and 8
D) 7 and 6
1. Which of the following algorithms is commonly used to find the majority element in an array in
O(n) time and O(1) space?
Options:
o A) QuickSort
o B) MergeSort
o D) Binary Search
2. What is the time complexity of the Boyer-Moore Voting Algorithm for finding the majority
element?
Options:
o A) O(n)
o B) O(n log n)
o C) O(n^2)
o D) O(log n)
3. Which of the following conditions must be true for an element to be considered a majority
element in an array of size n?
Options:
o A) It appears at least n/4 times.
4. Given the following Java code, what will be the output if the input array is [1, 2, 3, 2, 2, 1, 2, 2]?
java
Copy code
if (count == 0) {
candidate = num;
return candidate;
Options:
o A) 1
o B) 2
o C) 3
o D) 0
Correct Answer: B) 2
5. Which of the following Java code snippets correctly initializes the candidate and count variables
for the Boyer-Moore Voting Algorithm?
Options:
6. In the Boyer-Moore Voting Algorithm, what happens when the count variable becomes zero?
Options:
7. Given the following Java code, what will be the output if the input array is [2, 2, 1, 1, 1, 2, 2]?
java
Copy code
if (count == 0) {
candidate = num;
return candidate;
Options:
o A) 1
o B) 2
o C) 0
o D) -1
Correct Answer: B) 2
8. Consider the following array: [3, 3, 4, 2, 4, 4, 2, 4, 4]. Which element is the majority element
according to the Boyer-Moore Voting Algorithm?
Options:
o A) 2
o B) 3
o C) 4
o D) No majority element
Correct Answer: C) 4
9. Which Java method can be used to verify if a candidate is indeed the majority element after
using the Boyer-Moore Voting Algorithm?
Options:
o A) Collections.frequency()
o B) Arrays.binarySearch()
o C) Stream.count()
o D) Arrays.sort()
10. If an array contains multiple majority elements, which of the following statements is true for
the Boyer-Moore Voting Algorithm?
Options:
Options:
o A. madam
o B. civic
o C. aibohphobia
o D. radar
Options:
o A. equals()
o B. compareTo()
o C. charAt()
o D. substring()
java
Copy code
if (str.equals(sb.reverse().toString())) {
System.out.println("Palindrome");
} else {
System.out.println("Not Palindrome");
Options:
o A. Palindrome
o B. Not Palindrome
o C. Compile Error
o D. Runtime Error
Options:
o A. Two-pointer technique
o B. Recursion
o C. Using a stack
5. Given the string str = "aabbcc", what will be the lexicographically first palindromic string that
can be formed?
Options:
o A. abccba
o B. abcba
o C. accbca
6. Which of the following is the correct approach to find the lexicographically first palindromic
string in Java?
Options:
o B. Find all permutations of the string and check each for palindromes.
java
Copy code
String str = "aabb";
Arrays.sort(chars);
System.out.println(sortedStr);
Options:
o A. bbaa
o B. aabb
o C. abab
o D. baba
8. If a string contains an odd number of characters, which condition must be met to form a
palindromic string?
Options:
Correct Answer: B. All characters except one must occur an even number of times.
Explanation: A palindrome can have one character with an odd frequency (which will be in
the center) and all other characters must have even frequencies.
Options:
Options:
o A. Stack
o B. Queue
o C. HashMap
o D. LinkedList
1. Which of the following classes in Java implements the Comparable interface to define natural
ordering?
Options:
o A. HashMap
o B. ArrayList
o C. String
o D. LinkedList
Options:
o A. compareTo()
o B. compare()
o C. sort()
o D. order()
java
Copy code
Collections.sort(list);
System.out.println(list);
Options:
o A. [1, 2, 3]
o B. [3, 2, 1]
o C. [2, 3, 1]
o D. [1, 3, 2]
Correct Answer: A. [1, 2, 3]
Explanation: The Collections.sort() method sorts the list in ascending order, so the output will
be [1, 2, 3].
4. How does the compareTo method in the Comparable interface signal that one object is greater
than, equal to, or less than another?
Options:
o B. By throwing an exception
o C. By returning an integer
Options:
o A. HashSet
o B. TreeSet
o C. LinkedList
o D. Vector
java
Copy code
@Override
return this.name.compareTo(other.name);
Options:
7. Which of the following statements is true about the natural order of a class that implements
Comparable?
Options:
Options:
Correct Answer: B. Sorting operations may produce incorrect results or throw exceptions.
Explanation: If compareTo() is inconsistent (e.g., it doesn't properly order objects), sorting
operations might yield incorrect results or even throw exceptions.
Options:
10. Given the following class definition, what would be the result of sorting a list of Product
objects?
java
Copy code
String name;
double price;
this.name = name;
this.price = price;
}
@Override
Options:
1. Which of the following classes in Java implements the Comparable interface to define
natural ordering?
o A. HashMap
o B. ArrayList
o C. String
o D. LinkedList
Correct Answer: C. String
Explanation: The String class implements the Comparable interface to define its
natural ordering based on lexicographical order.
o A. compareTo()
o B. compare()
o C. sort()
o D. order()
Correct Answer: A. compareTo()
Explanation: To define the natural order of a class, it must implement the
compareTo() method from the Comparable interface.
Copy code
Collections.sort(list);
System.out.println(list);
o A. [1, 2, 3]
o B. [3, 2, 1]
o C. [2, 3, 1]
o D. [1, 3, 2]
Correct Answer: A. [1, 2, 3]
Explanation: The Collections.sort() method sorts the list in ascending order, so the
output will be [1, 2, 3].
4. How does the compareTo method in the Comparable interface signal that one object is
greater than, equal to, or less than another?
o B. By throwing an exception
o C. By returning an integer
o A. HashSet
o B. TreeSet
o C. LinkedList
o D. Vector
Correct Answer: B. TreeSet
Explanation: The TreeSet class uses the natural ordering of its elements, or an
ordering defined by a Comparator.
6. Given the following class definition, what is true about the compareTo method?
java
Copy code
@Override
return this.name.compareTo(other.name);
7. Which of the following statements is true about the natural order of a class that
implements Comparable?
8. What happens if a class that implements Comparable does not provide a consistent
compareTo method?
10. Given the following class definition, what would be the result of sorting a list of Product
objects?
java
Copy code
String name;
double price;
this.name = name;
this.price = price;
@Override
2. Given a string "abc" with weights {a: 1, b: 2, c: 3}, what is the weight of the substring "ab"?
o A) 1
o B) 2
o C) 3
o D) 4
Correct Answer: D) 4
Explanation: The weight of the substring "ab" is the sum of the weights of 'a' (1) and
'b' (2), giving a total of 3.
3. In the string "abcd", if each character has a weight equal to its position (1-based index),
what is the weight of the substring "cd"?
o A) 3
o B) 4
o C) 7
o D) 5
Correct Answer: C) 7
Explanation: The weight of the substring "cd" is the sum of the weights of 'c' (3) and
'd' (4), giving a total of 7.
4. Which algorithm can be used to efficiently find the maximum weighted substring in a
string?
o A) Brute force
o B) Dynamic programming
o C) Greedy algorithm
o D) Depth-first search
Correct Answer: B) Dynamic programming
Explanation: Dynamic programming is efficient in solving problems related to finding
the maximum weighted substring by breaking the problem into overlapping
subproblems.
5. If you have a string "xyz" and weights {x: 5, y: 4, z: 3}, what is the weight of the substring
"xyz"?
o A) 12
o B) 9
o C) 15
o D) 10
Correct Answer: A) 12
Explanation: The weight of the substring "xyz" is the sum of the weights of 'x' (5), 'y'
(4), and 'z' (3), giving a total of 12.
6. Given a string "aab" with weights {a: 2, b: 3}, what is the maximum weighted substring?
o A) "a"
o B) "aa"
o C) "b"
o D) "aab"
Correct Answer: B) "aa"
Explanation: The maximum weighted substring is "aa", with a total weight of 4 (2 +
2).
7. In the context of weighted substrings, what does the term "prefix sum" refer to?
o A) O(n)
o B) O(n^2)
o C) O(n^3)
o D) O(n^4)
Correct Answer: B) O(n^2)
Explanation: To find the weight of all substrings, we generate all pairs of start and
end indices of substrings, which results in O(n^2) complexity.
9. For the string "pqr" with weights {p: 7, q: 6, r: 5}, what is the total weight of all possible
substrings?
o A) 33
o B) 42
o C) 36
o D) 44
Correct Answer: B) 42
Explanation: The total weight is the sum of weights for all substrings: "p" (7), "q" (6),
"r" (5), "pq" (13), "qr" (11), "pqr" (18). The total is 7 + 6 + 5 + 13 + 11 + 18 = 42.
10. Which data structure can help in efficiently finding the maximum weighted substring in a
large string?
A) Array
B) Linked List
C) Segment Tree
D) Queue
Correct Answer: C) Segment Tree
Explanation: A segment tree is an efficient data structure that helps in answering range
queries and can be used to efficiently find the maximum weighted substring in large strings.
1. Which pivot selection strategy can help in avoiding the worst-case scenario in Quick Sort?
2. In Quick Sort, the process of dividing the array into two parts is called:
o A) Merging
o B) Dividing
o C) Partitioning
o D) Conquering
Correct Answer: C) Partitioning
Explanation: Partitioning is the process of dividing the array into two parts based on
the pivot element, where elements smaller than the pivot go to one side and
elements greater go to the other.
3. Which of the following methods is used in Quick Sort to choose a pivot element?
o A) Median of three
o B) First element
o C) Random element
o D) All of the above
Correct Answer: D) All of the above
Explanation: Quick Sort can use various methods like median of three, first element,
or a random element for selecting the pivot.
4. What is the primary advantage of using Quick Sort over other sorting algorithms?
o A) Simplicity of implementation
o B) Stability
o C) In-place sorting
5. In Quick Sort, if the input array is already sorted in ascending order, which pivot selection
strategy is most likely to cause the worst-case scenario?
o A) First element
o B) Last element
o C) Middle element
o D) Random element
Correct Answer: A) First element
Explanation: If the first element is selected as the pivot in a sorted array, it leads to
the worst-case scenario (O(n^2)) because the array is already partitioned in a skewed
way.
6. Which of the following statements is true about the partitioning process in Quick Sort?
7. What is a common strategy to handle the case when the input array has many duplicate
elements in Quick Sort?
o C) Quick Sort always uses the first element as pivot, Merge Sort uses the middle
element
o D) Quick Sort divides the array into two equal halves, Merge Sort does not
Correct Answer: B) Quick Sort is in-place, Merge Sort is not
Explanation: Quick Sort is an in-place algorithm, while Merge Sort requires additional
space to store the subarrays.
9. In which of the following scenarios is Quick Sort preferred over Merge Sort?
10. Which of the following is a correct way to implement the partition function in Quick Sort?
1. Which of the following is the correct way to move all hyphens to the beginning of a string
in Java?
2. Given a string str = "a-b-c-d", what will be the result of the following code?
java
Copy code
o A) abcd----
o B) ----abcd
o C) a--b--cd
o D) abcd
Correct Answer: A) abcd----
Explanation: First, str.replaceAll("-", "") removes all hyphens, producing "abcd", and
str.replaceAll("[^-]", "") removes all non-hyphen characters, leaving "----". The result
is "abcd----".
3. What is the time complexity of moving hyphens to the beginning of a string using the
replaceAll method in Java?
o A) O(n)
o B) O(n^2)
o C) O(n log n)
o D) O(1)
Correct Answer: B) O(n^2)
Explanation: The replaceAll method involves regular expression evaluation and string
copying, which results in a time complexity of O(n^2) due to the intermediate string
creation.
4. Which of the following methods can be used to move all hyphens to the beginning of a
string without using replaceAll?
o A) StringBuilder
o B) StringBuffer
o C) String
o D) Array
Correct Answer: A) StringBuilder
Explanation: StringBuilder can be used to efficiently build the string by appending
and prepending characters in a loop.
5. Given the string str = "a-b-c-d", which of the following snippets correctly moves all
hyphens to the beginning?
java
Copy code
if (ch == '-') {
sb.insert(0, ch);
} else {
sb.append(ch);
6. Which of the following regex patterns correctly matches all non-hyphen characters in a
string?
o A) [^-]
o B) [-]
o C) [^a-zA-Z0-9]
o D) [a-zA-Z0-9]
Correct Answer: A) [^-]
Explanation: The regex [^-] matches all characters that are not hyphens.
7. Given a string str = "-a-b-c-d-", what will be the result of the following code?
java
Copy code
o A) abcd----
o B) ----abcd
o C) a--b--c--d
o D) ----abcd----
Correct Answer: B) ----abcd
Explanation: First, str.replaceAll("-", "") removes all hyphens, producing "abcd", and
str.replaceAll("[^-]", "") removes all non-hyphen characters, leaving "----". The result
is "----abcd".
8. Which of the following Java collections is most suitable for storing the hyphens while
iterating through the string?
o A) ArrayList
o B) LinkedList
o C) HashSet
o D) TreeSet
Correct Answer: B) LinkedList
Explanation: LinkedList is most suitable for operations like inserting elements at the
beginning due to its efficient insertions.
java
Copy code
int count = 0;
if (ch == '-') {
count++;
} else {
sb.append(ch);
sb.insert(0, '-');
System.out.println(sb.toString());
o A) abcd----
o B) ----abcd
o C) a--b--cd
o D) abcd
Correct Answer: B) ----abcd
Explanation: This code counts the hyphens, removes them, and then inserts the
same number of hyphens at the beginning.
10. What does the replaceAll("[^-]", "") part of the code do?
o D) Sorting an array
Answer: B) Longest palindromic substring
o A) Stack
o B) Queue
o C) Hash table
o A) O(n)
o B) O(n log n)
o C) O(n^2)
o D) O(n^3)
Answer: A) O(n)
o A) Prefix function
o C) Suffix array
o A) Dynamic programming
o B) Two-pointer technique
o A) O(1)
o B) O(n)
o C) O(n^2)
o D) O(n log n)
Answer: B) O(n)
o A) 1975
o B) 1989
o C) 1995
o D) 2005
Answer: B) 1989
2. How many sorted unique permutations can be generated from the string "ABC"?
o A) 3
o B) 6
o C) 9
o D) 12
Answer: B) 6
o D) Dynamic programming
Answer: A) Depth-first search (DFS)
4. What is the time complexity for generating all sorted unique permutations of a string of
length n using backtracking?
o A) O(n)
o B) O(n log n)
o C) O(n!)
o D) O(2^n)
Answer: C) O(n!)
5. In which order are sorted unique permutations typically generated using backtracking?
o A) Random order
o C) Lexicographical order
o D) No specific order
Answer: C) Lexicographical order
6. Which data structure is suitable for efficiently checking and maintaining used characters
during permutation generation?
o A) Stack
o B) Queue
o C) Set
o D) Array
Answer: C) Set
7. What is the key property of sorted unique permutations that distinguishes them from all
possible permutations?
o A) Hashing
o B) Sorting
o C) Backtracking
o D) Dynamic programming
Answer: C) Backtracking
10. What is the space complexity for generating all sorted unique permutations of a string of
length n using backtracking?
o A) O(1)
o B) O(n)
o C) O(n!)
o D) O(2^n)
Answer: C) O(n!)
1. In military terms, what does the term "maneuvering" primarily refer to?
o A) Setting up camp
o B) Planning logistics
o A) Tailgating
o B) Anticipating hazards
o C) Speeding up to pass obstacles
4. Which term is used to describe a sudden change in direction or tactic to gain an advantage
in a competitive situation?
o A) Pivot
o B) Retreat
o C) Maneuver
o D) Stagnation
Answer: C) Maneuver
o D) Avoiding engagements
Answer: C) Gaining a tactical advantage
6. Which driving maneuver involves reversing the vehicle to change its direction?
o A) U-turn
o B) Parallel parking
o C) Three-point turn
o D) J-turn
Answer: C) Three-point turn
7. In naval warfare, what does "maneuvering" typically refer to?
o A) Firing cannons
o B) Sailing tactics
o C) Anchoring ships
o D) Repairing hulls
Answer: B) Sailing tactics
8. Which factor is crucial for effective maneuvering in team sports such as basketball or
soccer?
o B) Team communication
o C) Timekeeping
o D) Spectator support
Answer: B) Team communication
o D) Climbing alone
Answer: C) Using proper safety equipment
10. Which of the following statements about maneuvering is true across various contexts?
1. In how many ways can you choose 3 books from a shelf containing 7 different books?
o A) 21
o B) 35
o C) 42
o D) 84
Answer: A) 21
(This is a combination problem: (73)=7×6×53×2×1=21\binom{7}{3} = \frac{7 \times 6
\times 5}{3 \times 2 \times 1} = 21(37)=3×2×17×6×5=21)
o A) 252
o B) 210
o C) 120
o D) 2520
Answer: B) 210
(This is a combination problem: (105)=10×9×8×7×65×4×3×2×1=210\binom{10}{5} =
\frac{10 \times 9 \times 8 \times 7 \times 6}{5 \times 4 \times 3 \times 2 \times 1} =
210(510)=5×4×3×2×110×9×8×7×6=210)
3. How many different 4-letter combinations can be formed from the letters A, B, C, D, E, F
without repetition?
o A) 120
o B) 360
o C) 720
o D) 24
Answer: B) 360
(This is a permutation problem: P(6,4)=6!(6−4)!=6×5×4×31=360P(6,4) = \frac{6!}{(6-
4)!} = \frac{6 \times 5 \times 4 \times 3}{1} = 360P(6,4)=(6−4)!6!=16×5×4×3=360)
4. How many ways can you arrange the letters in the word "COMBINATIONS"?
o A) 5040
o B) 3628800
o C) 40320
o D) 1440
Answer: B) 3628800
(There are 11 letters with repeated "O", "I", "N", "T" which result in this calculation:
11!2!2!2!2!\frac{11!}{2!2!2!2!}2!2!2!2!11!)
5. In a lottery, 5 numbers are to be picked from 1 to 50. How many different combinations of
numbers can be chosen?
o A) 2118760
o B) 1024
o C) 3125
o D) 2598960
Answer: A) 2118760
(This is a combination problem:
(505)=50×49×48×47×465×4×3×2×1=2118760\binom{50}{5} = \frac{50 \times 49
\times 48 \times 47 \times 46}{5 \times 4 \times 3 \times 2 \times 1} = 2118760(550
)=5×4×3×2×150×49×48×47×46=2118760)
6. How many ways can you select 2 red marbles and 3 blue marbles from a collection of 10
red and 15 blue marbles?
o A) 210
o B) 3003
o C) 252
o D) 126
Answer: C) 252
(This is a combination problem: (102)×(153)=45×455=252\binom{10}{2} \times
\binom{15}{3} = 45 \times 455 = 252(210)×(315)=45×455=252)
7. A password consists of 4 digits where the first digit cannot be 0. How many different
passwords are possible?
o A) 9000
o B) 7290
o C) 6480
o D) 6561
Answer: A) 9000
(For the first digit, there are 9 options (1-9) and for each of the next three digits,
there are 10 options (0-9): 9×10×10×10=90009 \times 10 \times 10 \times 10 =
90009×10×10×10=9000)
8. How many different 7-digit telephone numbers can be formed if each number must start
with 5 and no digit can be repeated?
o A) 720
o B) 5040
o C) 362880
o D) 1440
Answer: A) 720
(The first digit is fixed as 5, so there are 9 options for the second digit, 8 options for
the third, and so on: 9×8×7×6×5×4×3=7209 \times 8 \times 7 \times 6 \times 5
\times 4 \times 3 = 7209×8×7×6×5×4×3=720)
9. In how many ways can you distribute 8 identical chocolates among 4 children such that
each child gets at least one chocolate?
o A) 70
o B) 35
o C) 56
o D) 84
Answer: A) 70
(This is a stars and bars problem: (8−14−1)=(73)=35\binom{8-1}{4-1} = \binom{7}{3}
= 35(4−18−1)=(37)=35)
10. How many different ways can you arrange the letters in the word "SUCCESS"?
o A) 2520
o B) 420
o C) 720
o D) 210
Answer: B) 420
(There are 7 letters with repeated "S" (3 times) and "C" (2 times):
7!3!2!=50406×2=420\frac{7!}{3!2!} = \frac{5040}{6 \times 2} = 4203!2!7!=6×25040
=420)
1. In the classic Josephus problem where every second person is eliminated in a circle until
only one remains, if there are 12 people, which position would be the last person
standing?
o A) 7
o B) 9
o C) 11
o D) 12
Answer: B) 9
(Using the Josephus problem formula, the position of the last person standing for 12
people where every second person is eliminated is 9.)
2. In a circle of 20 people playing the Josephus game (every 3rd person is eliminated), what
position would the last remaining person be?
o A) 9
o B) 12
o C) 16
o D) 19
Answer: C) 16
(The Josephus problem for 20 people and eliminating every 3rd person results in
position 16 being the last remaining person.)
o A) Graph theory
o B) Number theory
o C) Game theory
o D) Combinatorics
Answer: C) Game theory
(The Josephus problem is a problem in game theory that involves elimination and
strategy in a circular setup.)
4. If there are 15 people standing in a circle and every 4th person is eliminated until only one
remains, which position is safe from elimination?
o A) 9
o B) 11
o C) 12
o D) 15
Answer: B) 11
(For 15 people, eliminating every 4th person, position 11 is the safe one.)
5. In the Josephus problem with 10 people where every 2nd person is eliminated, what is the
safe position?
o A) 4
o B) 5
o C) 7
o D) 10
Answer: B) 5
(In the Josephus problem for 10 people with every second person eliminated,
position 5 is the safe one.)
o A) Algebra
o B) Geometry
o C) Number theory
o D) Probability theory
Answer: C) Number theory
(The Josephus problem involves understanding patterns and relationships, which are
closely tied to number theory.)
7. If there are 25 people in a circle and every 5th person is eliminated, which position will be
the last remaining?
o A) 13
o B) 17
o C) 19
o D) 23
Answer: B) 17
(For 25 people, eliminating every 5th person, position 17 is the last remaining
person.)
8. In the Josephus problem with 8 people and every 3rd person being eliminated, which
position is the last remaining person?
o A) 4
o B) 6
o C) 7
o D) 8
Answer: C) 7
(With 8 people and eliminating every 3rd person, position 7 is the last remaining.)
o A) Cryptography
o B) Sorting algorithms
o C) Data compression
o D) None of the above
Answer: D) None of the above
(The Josephus problem is a theoretical problem used to demonstrate elimination
patterns and does not have direct applications in cryptography, sorting, or data
compression.)
10. If there are 30 people standing in a circle and every 6th person is eliminated, which
position will be the last person standing?
o A) 12
o B) 16
o C) 18
o D) 30
Answer: B) 16
(For 30 people and eliminating every 6th person, position 16 will be the last
remaining person.)
1. What is a common algorithm used for solving mazes with a single solution path?
o C) Dijkstra's Algorithm
o D) A* Search Algorithm
Answer: A) Depth-First Search (DFS)
(DFS is commonly used for maze-solving when there is a single solution path, as it
explores each path deeply before backtracking.)
2. Which data structure is typically used to implement DFS and BFS for maze solving?
o A) Queue
o B) Stack
o C) Priority Queue
o D) Linked List
Answer: A) Queue (for BFS)
Answer: B) Stack (for DFS)
(BFS uses a queue, while DFS uses a stack to manage the exploration of nodes in the
maze.)
o C) Dijkstra's Algorithm
o D) A* Search Algorithm
Answer: B) Breadth-First Search (BFS)
(BFS guarantees the shortest path in an unweighted maze as it explores all nodes
level by level.)
4. Which heuristic is commonly used in the A search algorithm for maze solving?*
o A) Manhattan distance
o B) Euclidean distance
o C) Hamming distance
o D) Chebyshev distance
Answer: A) Manhattan distance
(Manhattan distance is commonly used in grid-based mazes, where movements are
restricted to horizontal and vertical directions.)
6. Which traversal technique is more suitable for finding all possible solutions in a maze?
o C) Dijkstra's Algorithm
o D) A* Search Algorithm
Answer: A) Depth-First Search (DFS)
(DFS explores as deeply as possible into the maze, thus allowing it to find all possible
solutions.)
7. Which algorithm is preferred for finding the shortest path in a weighted maze?
o A) Depth-First Search (DFS)
o C) Dijkstra's Algorithm
o D) A* Search Algorithm
Answer: C) Dijkstra's Algorithm
(Dijkstra's algorithm is specifically designed to handle weighted graphs, making it
suitable for weighted mazes.)
8. What is the time complexity of Breadth-First Search (BFS) for maze solving?
o A) O(V)
o B) O(E)
o C) O(V + E)
o D) O(V^2)
Answer: C) O(V + E)
(The time complexity of BFS is O(V + E), where V is the number of vertices and E is
the number of edges.)
9. Which of the following algorithms is not suitable for maze solving that involves finding the
shortest path?
o C) Dijkstra's Algorithm
o D) A* Search Algorithm
Answer: B) Depth-First Search (DFS)
(DFS does not guarantee the shortest path, as it explores one path fully before
backtracking, which may lead to suboptimal solutions.)
10. Which algorithm is generally more efficient when a heuristic can be defined for the maze-
solving problem?
o C) Dijkstra's Algorithm
o D) A* Search Algorithm
Answer: D) A Search Algorithm*
(A* search algorithm is designed to be efficient when a heuristic can be used to
estimate the cost of reaching the goal.)
In how many directions do queens attack each other?
A) 1
B) 2
C) 3
D) 4
Answer: D) 4
A) n-queen’s problem
B) 8-queen’s problem
A) Carom
B) Chess
C) Ludo
D) Cards
Answer: B) Chess
In n-queen problem, how many values of n does not provide an optimal solution?
A) 1
B) 2
C) 3
D) 4
Answer: B) 2
(For n = 2 and n = 3, there is no solution.)
A) Greedy algorithm
C) Iterative improvement
D) Backtracking
Answer: D) Backtracking
Of the following given options, which one of the following is a correct option that provides an
optimal solution for the 4-queens problem?
A) (3,1,4,2)
B) (2,3,1,4)
C) (4,3,2,1)
D) (4,2,3,1)
Answer: A) (3,1,4,2)
A) 100
B) 98
C) 92
D) 88
Answer: C) 92
A) 850
B) 742
C) 842
D) 724
Answer: D) 724
A) True
B) False
Answer: A) True
A) 8
B) 7
C) 6
D) 5
Answer: A) 8
A) Choosing the move that leads to the highest degree in the subsequent move
B) Choosing the move closest to the board's center
Answer: A) Choosing the move that leads to the highest degree in the subsequent move
Why is Warnsdorff's algorithm considered a heuristic method rather than an exact algorithm?
B) Because it may not always find a solution, especially for larger boards
Answer: B) Because it may not always find a solution, especially for larger boards
What is the role of the "degree of a vertex" in the context of Warnsdorff's algorithm?
A) It represents the number of possible moves a knight can make from that position
Answer: A) It represents the number of possible moves a knight can make from that
position
C) It ensures that the knight always moves to the closest available square
Answer: B) It prioritizes moves that minimize the risk of getting trapped early
What is the difference between an "open" and a "closed" Knight's Tour in the context of
Warnsdorff's algorithm?
A) An open tour starts and ends on different squares, while a closed tour starts and ends on
the same square
B) An open tour uses all squares of the board, while a closed tour does not
C) An open tour allows revisiting squares, while a closed tour does not
D) An open tour is solved using Warnsdorff's algorithm, while a closed tour is not
Answer: A) An open tour starts and ends on different squares, while a closed tour starts
and ends on the same square
How can Warnsdorff's algorithm be modified to increase the likelihood of finding a solution on
larger boards?
D) It fails to find a solution when starting from the corner of the board
In computational complexity terms, why is Warnsdorff's algorithm often preferred over brute
force approaches for the Knight's Tour problem?
When applied to non-standard board sizes (not 8x8), what is the effect on the efficiency and
effectiveness of Warnsdorff's algorithm?
Answer: B) The algorithm's effectiveness can vary, sometimes failing to find a solution
A) A Hamiltonian cycle includes all vertices exactly once, while a Hamiltonian path may
repeat vertices.
B) A Hamiltonian cycle forms a closed loop, while a Hamiltonian path does not return to the
starting vertex.
C) A Hamiltonian cycle only exists in directed graphs, while a Hamiltonian path exists in
undirected graphs.
Answer: B) A Hamiltonian cycle forms a closed loop, while a Hamiltonian path does not
return to the starting vertex.
A) P
B) NP
C) NP-hard
D) NP-complete
Answer: D) NP-complete
Which algorithmic approach is typically ineffective for finding Hamiltonian cycles in large graphs
due to its exponential time complexity?
A) Dynamic programming
B) Greedy algorithm
C) Brute force
D) Backtracking
Consider Dirac's theorem in graph theory. What does it state regarding the Hamiltonian cycle?
A) A graph with n vertices is Hamiltonian if every vertex has degree at least n2\frac{n}{2}2n.
Answer: A) A graph with n vertices is Hamiltonian if every vertex has degree at least
n2\frac{n}{2}2n.
In directed graphs, what special type of Hamiltonian cycle is called a "Hamiltonian circuit"?
Which of the following graph classes is known to always contain a Hamiltonian cycle?
A) Planar graphs
B) Bipartite graphs
C) Complete graphs
D) Trees
What is the Travelling Salesman Problem (TSP) in relation to the Hamiltonian cycle?
B) It is a variation where the Hamiltonian cycle needs to have the minimum possible weight.
B) It states that a graph is Hamiltonian if the sum of the degrees of any two non-adjacent
vertices is at least the number of vertices in the graph.
C) It provides a necessary and sufficient condition for the existence of Hamiltonian cycles in
planar graphs.
C) greedy algorithm
D) approximation algorithm
What is the weight of the minimum spanning tree using the Kruskal’s algorithm?
(Note: Without the graph, I can't determine the exact weight. Please provide the graph if needed.)
A) O(log V)
B) O(E log V)
C) O(E²)
D) O(V log E)
D) In Kruskal’s sort edges are added to MST in decreasing order of their weights
A) It is a greedy algorithm
Kruskal’s algorithm is best suited for dense graphs than Prim’s algorithm.
A) True
B) False
Answer: B) False
S2: Kruskal’s algorithm can be efficiently implemented using the disjoint-set data structure.
1. Which of the following approaches should be used to find the solution of the activity
selection problem?
o A) Greedy approach
o C) Brute-force approach
o D) Dynamic programming
2. Suppose two activities A and B, having start and finish time as SA, FA, and SB, FB
respectively. Both the activities are said to be compatible, under which of the following
conditions?
o A) SA = FB
o B) SA > FB
o C) SA ≥ FB or SB ≥ FA
o D) SA ≥ FB and SB = FA
o Answer: D) SA ≥ FB and SB = FA
3. Consider the following algorithm to find the solution of the activity selection problem.
Which of the following options is best suited to fill the blank?
D) increment j
5. Time complexity to find the solution of the activity selection problem takes O(n log n)
time, when the list is not sorted.
o A) True
o B) False
o Answer: B) False (The time complexity is O(n log n) only if the list is sorted,
otherwise O(n²) for brute force).
6. The solution of the activity selection problem can have two overlapping activities in a
particular time interval.
o A) True
o B) False
7. Consider the following number of activities with their start and finish time given below.
Which of the following activity will be left out?
(Note: Without the graph, I can't determine the correct activity. Please provide the graph if
needed.)
8. Which among the following represents best-case time complexity for the activity selection
problem?
o A) O(n)
o B) O(1)
o C) O(n²)
o D) O(n² * log n)
o Answer: A) O(n) (Best case complexity when the activities are already sorted).
o A) I, II
o B) I, II, III
o C) III ONLY
o D) I only
o Answer: D) I only (A minimum cost spanning tree can be solved with a greedy
algorithm, but the other problems require different approaches).
10. The minimum number of colors with which we can guarantee that no two adjacent nodes
will have the same color if we want to color the nodes of any cycle is:
A) 2
B) 3
C) 4
D) n+2
Answer: A) 2 (For any cycle, 2 colors are sufficient, as the cycle is bipartite).
1. What is the minimum number of colors required to color a graph such that no two
adjacent vertices have the same color called?
o A) Chromatic number
o B) Chromatic index
o C) Coloring number
2. In a graph, what is the condition for two vertices to be assigned different colors in a proper
vertex coloring?
3. Which of the following statements is true for a bipartite graph regarding vertex coloring?
o A) 1
o B) 2
o C) n
o D) n - 1
5. Which algorithm is commonly used for finding an approximate solution to the vertex
coloring problem in polynomial time?
o D) Dijkstra's algorithm
o A) Chromatic number
o B) Chromatic index
7. What is the maximum number of colors required to color the edges of any simple graph G
according to Vizing's theorem?
o B) Δ(G) - 1
o C) ⌈Δ(G)/2⌉
o D) Δ(G)
o Answer: A) Δ(G) + 1
8. What type of graph always requires at least three colors for proper coloring?
o A) Planar graph
o B) Bipartite graph
o C) Non-bipartite graph
o A) It states that any planar graph can be colored with at most four colors.
o Answer: A) It states that any planar graph can be colored with at most four colors.
Which of the following Java classes can be used to implement a min-heap for Huffman Coding?
A) Stack
B) PriorityQueue
C) ArrayList
D) LinkedList
Answer: B) PriorityQueue
When constructing the Huffman Tree, what happens to the two nodes with the lowest
frequencies?
B) An encoded string
D) A sorted array
A) Simplicity
B) Speed
C) Efficiency in space
D) Consistency
Which data structure is used to store the Huffman codes for each character?
A) Linked list
B) Array
C) HashMap
D) Stack
Answer: C) HashMap
A) O(n)
B) O(n log n)
C) O(n^2)
D) O(log n)
B) Using an array
C) Using a min-heap
B) Data compression
A) IOException
B) NullPointerException
C) SQLException
D) ClassNotFoundException
Answer: A) IOException
A) ServerSocket
B) Socket
C) DatagramSocket
D) InetAddress
Answer: A) ServerSocket
Which method is used to accept a connection from a client in the ServerSocket class?
A) connect()
B) bind()
C) accept()
D) listen()
Answer: C) accept()
A) ServerSocket
B) Socket
C) DatagramSocket
D) InetAddress
Answer: B) Socket
A) TCP
B) FTP
C) HTTP
D) UDP
Answer: D) UDP
B) ServerSocket
C) InetAddress
D) DatagramPacket
Answer: C) InetAddress
Which method of the Socket class is used to get the input stream for reading data from the
socket?
A) getOutputStream()
B) getInputStream()
C) connect()
D) close()
Answer: B) getInputStream()
Which method of the Socket class is used to send data to the socket?
A) getInputStream()
B) getOutputStream()
C) send()
D) write()
Answer: B) getOutputStream()
A) 21
B) 25
C) 80
D) 443
Answer: C) 80
Which of the following is not a valid constructor for the DatagramPacket class?
a) SecureRandom
b) KeyPairGenerator
c) Cipher
d) MessageDigest
Answer: c) Cipher
a) AES
b) DES
c) RSA
d) Blowfish
Answer: c) RSA
c) Using a public key for encryption and a private key for decryption
Answer: a) Using the same key for both encryption and decryption
Which Java library provides cryptographic operations, including encryption and decryption?
a) java.util
b) java.io
c) java.security
d) java.net
Answer: c) java.security
Which method of the Cipher class initializes the cipher for encryption or decryption?
a) init()
b) start()
c) setup()
d) initialize()
Answer: a) init()
What is a common block cipher mode of operation used with the AES algorithm in Java?
Which method of the Cipher class is used to perform the actual encryption or decryption?
a) doFinal()
b) update()
c) process()
d) execute()
Answer: a) doFinal()