Explore topic-wise MCQs in Java.

This section includes 144 Mcqs, each offering curated multiple-choice questions to sharpen your Java knowledge and support exam preparation. Choose a topic below to get started.

51.

Consider the following recursive implementation of linear search:Which of the following recursive calls should be added to complete the below code?

A. recursive_search_num(arr, num+1, idx, len);
B. recursive_search_num(arr, num, idx, len);
C. recursive_search_num(arr, num, idx+1, len);
D. recursive_search_num(arr, num+1, idx+1, len);
Answer» D. recursive_search_num(arr, num+1, idx+1, len);
52.

How many times is the function recursive_search_num() called when the following code is executed?

A. 5
B. 6
C. 7
D. 8
Answer» C. 7
53.

Consider the following code snippet to search an element in a linked list:Which of the following lines should be inserted to complete the below code?

A. temp = next
B. temp->next = temp
C. temp = temp->next
D. return 0
Answer» D. return 0
54.

Consider the following iterative implementation used to find the length of a linked list:Which of the following conditions should be checked to complete thebelow code?

A. temp->next != 0
B. temp == 0
C. temp != 0
D. none of the mentioned
Answer» D. none of the mentioned
55.

Which of the following lines should be inserted to complete the following recursive implementation used to find the length of a linked list?

A. recursive_get_len(current_node)
B. 1 + recursive_get_len(current_node)
C. recursive_get_len(current_node->next)
D. 1 + recursive_get_len(current_node->next)
Answer» E.
56.

Consider the following iterative implementation to find the length of the string:Which of the following lines should be inserted to complete thebelow code?

A. s[len-1] != 0
B. s[len+1] != 0
C. s[len] != ‘\0’
D. none of the mentioned
Answer» D. none of the mentioned
57.

Consider the following iterative implementation used to reverse a string:Which of the following lines should be inserted to complete thebelow code?

A. i > j
B. i < len
C. j > 0
D. i < j
Answer» E.
58.

How many times is the function recursive_sum_of_digits() called when the following code is executed?

A. 6
B. 7
C. 8
D. 9
Answer» D. 9
59.

You have to find the sum of digits of a number given that the number is always greater than 0. Which of the following base cases can replace the base case for the below code?

A. if(n == 0) return 1
B. if(n == 1) return 0
C. if(n == 1) return 1
D. none of the mentioned
Answer» E.
60.

Consider the following iterative implementation to find the sum of digits of a number:Which of the following lines should be inserted to complete thebelow code?

A. sm += n
B. sm += n%10
C. sm += n-10
D. sm += n/10
Answer» C. sm += n-10
61.

Consider the following recursive implementation to find the sum of digits of number:Which of the following lines should be inserted to complete thebelow code?

A. (n / 10) + recursive_sum_of_digits(n % 10)
B. (n) + recursive_sum_of_digits(n % 10)
C. (n % 10) + recursive_sum_of_digits(n / 10)
D. (n % 10) + recursive_sum_of_digits(n % 10)
Answer» D. (n % 10) + recursive_sum_of_digits(n % 10)
62.

How many times is the function recursive_dec_to_bin() called when the following code is executed?

A. 7
B. 8
C. 9
D. 10
Answer» C. 9
63.

Consider the following code:Which of the following lines is the base case for thebelow code?

A. if(n ==0 && len == 0)
B. if(n == 0)
C. if(n ==0 && len == 0) and if(n == 0)
D. none of the mentioned
Answer» D. none of the mentioned
64.

Consider the following iterative code used to convert a decimal number to its equivalent binary:Which of the following lines should be inserted to complete thebelow code?

A. n–
B. n /= 2
C. n /= 10
D. n++
Answer» C. n /= 10
65.

Consider the following recursive implementation used to convert a decimal number to its binary equivalent:Which of the following lines should be inserted to complete thebelow code?

A. arr[len] = n
B. arr[len] = n % 2
C. arr[len++] = n % 2
D. arr[len++] = n
Answer» D. arr[len++] = n
66.

How many times is the function recursive_sum() called when the following code is executed?

A. 4
B. 5
C. 6
D. 7
Answer» D. 7
67.

Consider the following code:Which of the following is the base case for the above recursive code?

A. if(n == 0)
B. return 0
C. return n + recursive_sum(n – 1)
D. none of the mentioned
Answer» B. return 0
68.

How many times is the function recursive_binary_search() called when the following code is executed?

A. 0
B. 1
C. 2
D. 3
Answer» D. 3
69.

Consider the following recursive implementation of the binary search. Which of the following lines should be added to complete the below code?

A. hi = mid – 1
B. mid = (lo + hi)/2
C. mid = lo – 1
D. lo = mid + 1
Answer» E.
70.

In which of the below cases will the following code produce a wrong output?

A. Array: {0,0,0,0,0,0} Search: -10
B. Array: {1,2,3,4,5} Search: 0
C. Array: {5,4,3,2,1} Search: 1
D. Array: {-5,-4,-3,-2,-1} Search: -1
Answer» D. Array: {-5,-4,-3,-2,-1} Search: -1
71.

What is the base case for the following code?

A. return
B. printf(“%d “, n)
C. if(n == 0)
D. my_recursive_function(n-1)
Answer» D. my_recursive_function(n-1)
72.

How many times is the recursive function called, when the following code is executed?

A. 9
B. 10
C. 11
D. 12
Answer» D. 12
73.

What does the following recursive code do?

A. Prints the numbers from 10 to 1
B. Prints the numbers from 10 to 0
C. Prints the numbers from 1 to 10
D. Prints the numbers from 0 to 10
Answer» D. Prints the numbers from 0 to 10
74.

Consider the following code snippet.What will happen when the above snippet is executed?

A. The code will be executed successfully and no output will be generated
B. The code will be executed successfully and random output will be generated
C. The code will show a compile time error
D. The code will run for some time and stop when the stack overflows
Answer» E.
75.

What will be the output of the following code?

A. True
B. False
C. Error
D. None of the above
Answer» C. Error
76.

Consider the following recursive implementation of linear search on a linked list:Which of the following lines should be inserted to complete the below code?

A. 1
B. 0
C. linear_search(temp, value)
D. linear_search(temp->next, value)
Answer» E.
77.

How many times is the function linear_search() called when the following code is executed?

A. 5
B. 6
C. 7
D. 8
Answer» C. 7
78.

How many times is the function recursive_reverse_string() called when the following code is executed?

A. 3
B. 4
C. 5
D. 6
Answer» B. 4
79.

What does the following code do?

A. Adds all the indexes of the number 0
B. Finds the first last occurrence of the number 0
C. Counts the number of occurrences of the number 0
D. None of the mentioned
Answer» D. None of the mentioned
80.

Consider the following recursive implementation used to reverse a string:.Which of the following lines should be inserted to complete the below code?

A. recursive_reverse_string(s, left+1, right+1)
B. recursive_reverse_string(s, left-1, right-1)
C. recursive_reverse_string(s, left+1, right-1)
D. recursive_reverse_string(s, left-1, right+1)
Answer» E.
81.

Consider the following iterative solution to find the sum of first n natural numbers:Which of the following lines completes thebelow code?

A. sm = i
B. sm += i
C. i = sm
D. i += sm
Answer» C. i = sm
82.

Consider the following recursive implementation to find the factorial of a number:Which of the following lines should be inserted to complete thebelow code?

A. n = 0
B. n != 0
C. n == 0
D. n == 1
Answer» D. n == 1
83.

Consider the following code:Which of the following lines is the recurrence relation for thebelow code?

A. (n – 1) +recursive_sum(n)
B. n + recursive_sum(n)
C. n + recursive_sum(n – 1)
D. (n – 1) + recursive_sum(n – 1)
Answer» D. (n – 1) + recursive_sum(n – 1)
84.

How many times will the function fibo() be called when the following code is executed?

A. 5
B. 6
C. 8
D. 9
Answer» E.
85.

Consider the following recursive implementation to find the factorial of a number:Which of the following lines is the base case?

A. return 1
B. return n * fact(n-1)
C. if(n == 0)
D. none of the mentioned
Answer» D. none of the mentioned
86.

How many times will the function fact() be called when the following code is executed?

A. 4
B. 5
C. 6
D. 7
Answer» D. 7
87.

Consider the following iterative implementation to find the factorial of a number:Which of the following lines should be inserted to complete thebelow code?

A. fact = fact + i
B. fact = fact * i
C. i = i * fact
D. i = i + fact
Answer» C. i = i * fact
88.

Consider the following iterative implementation to find the nth fibonacci number:Which of the following lines should be added to complete thebelow code?

A. c = bb = a
B. a = bb = c
C. b = ca = b
D. a = bb = a
Answer» C. b = ca = b
89.

Consider the following recursive implementation to find the nth fibonacci number:Which of the following lines should be inserted to complete thebelow code?

A. fibo(n – 1)
B. fibo(n – 1) + fibo(n – 2)
C. fibo(n) + fibo(n – 1)
D. fibo(n – 2) + fibo(n – 1)
Answer» C. fibo(n) + fibo(n – 1)
90.

Consider the following recursive implementation to find the nth fibonnaci number:Which of the following is the base case?

A. if(n == 1)
B. else if(n == 2)
C. return fibo(n – 1) + fibo(n – 2)
D. both if(n == 1) and else if(n == 2)
Answer» E.
91.

Two or more functions, which invoke (call) each other, are called_________________.

A. combined recursive functions
B. coexitant recursive functions
C. mutually recursive functions
D. double recursive functions
Answer» D. double recursive functions
92.

It is necessary to declare the type of a function in the calling program if the function

A. Returns an integer
B. Returns a non-integer value
C. Is not defined in the same file
D. None of these
Answer» C. Is not defined in the same file
93.

A function, which invokes itself repeatedly until some condition is satisfied, is called a_________.

A. Recursive function
B. Big Theta θ(f)
C. Big Omega Ω(f)
D. None of the above
Answer» B. Big Theta θ(f)
94.

________________ is a powerful technique which is used to invoke a function.

A. Computing
B. Recursion
C. Big O
D. None of the above
Answer» C. Big O
95.

A recursive function is often less efficient compared to an iterative function. But it is more________.

A. Elegant
B. Complex
C. Simple
D. None of the above
Answer» B. Complex
96.

Which of the following recurrence relations can be used to find the nth fibonacci number?

A. F(n) = F(n) + F(n – 1)
B. F(n) = F(n) + F(n + 1)
C. F(n) = F(n – 1)
D. F(n) = F(n – 1) + F(n – 2)
Answer» E.
97.

A recursive agorithm must have

A. 1 part
B. 2 parts
C. 3 parts
D. 4 parts
Answer» C. 3 parts
98.

What is a good approach to remove recursion from an algorithm?

A. Recursion
B. Stack implementation
C. Regressive
D. None of the above
Answer» C. Regressive
99.

Which of the following statements is true ?

A. Recursion is always better than iteration
B. Recursion uses more memory compared to iteration
C. Recursion uses less memory compared to iteration
D. Iteration is always better and simpler than recursion
Answer» C. Recursion uses less memory compared to iteration
100.

The number of recursive calls is limited to the _____ of the stack.

A. Time
B. Ability
C. Quality
D. Size
Answer» E.