![](https://sp-ao.shortpixel.ai/client/to_webp,q_glossy,ret_img,w_996,h_267/https://codewindow.in/wp-content/uploads/2022/05/WhatsApp-Image-2022-04-30-at-3.29.47-PM.jpeg)
void func(Queue q)
{
int i;
if(!isEmpty(q))
{
i = delete(q);
func(q);
insert(q,i);
}
}
Question 1
Answer
Question 1
Given a Queue implementation with insert, delete and isEmpty methods, what does the above function do?
A. q is unchanged
B. q is reversed
C. q is empty
D. None
Answer
B. q is reversed
String n_squares(int n){
StringBuffer s = new StringBuffer("0");
int n_squared = n*n;
for (int i = 0; i<n_squared; i++) {
s.append(", "+i);
}
return s.toString();
}
Question 2
Answer
Question 2
If a call to n_squares(1000) takes 1 second to return, the time taken for n_squares(2000) is likely to be closest to:
A. 1.4142 seconds
B. 2 seconds
C. 1 seconds
D. 4 seconds
E. 1.5 seconds
Answer
D. 4 seconds
Question 3
Answer
Question 3
Necessity of overriding clone method comes when
A. When we want to call clone our object from different class
B. When we want the different behavior of clone method
C. When we have more decendents of our class
D. None of the above
Answer
B. When we want the different behavior of clone method
Question 4
Answer
Question 4
What is true about a weakHashmap.
A. Garbage collector will automatically remove the entry from it.
B. works same as Hashmap, only difference is weakHashMap is synchronized and legacy.
C. Entry from WeakHashMap will never be removed by Garbage collector.
D. None of the above.
Answer
C. Entry from WeakHashMap will never be removed by Garbage collector.
void function(struct node* head){
if(head == NULL)
return;
function(head->next);
printf("%d ", head->data);
}
Question 5
Answer
Question 5
What will be output of function(head) for given linked list 10->20->30->40->50 with first node as head:
A. 10 20 30 40
B. 50 40 30 20 10
C. 50 40 30 20
D. 10 20 30 40 50
Answer
B. 50 40 30 20 10
Question 6
Answer
Question 6
What can contain a mixture of implemented and unimplemented methods and must be extended to use?
A. Concrete class
B. Abstract class
C. Java Class
D. Interface
Answer
B. Abstract class
Question 7
Answer
Question 7
Class variables, also known as static fields, have only one instance in existence. Following standing naming conventions, which answer represents a class variable?
A. variableName
B. ClassName:variableName
C. VariableName
D. ClassName.variableName
Answer
D. ClassName.variableName
void main() /* Assume array begins at 1200 */
{
int arr[] = {2, 3, 4, 1, 6}
printf("%u %d", arr, sizeof(arr));
}
Question 8
Answer
Question 8
What is the output of the above program
A. 1200 10
B. 1200 5
C. 1210 10
D. None of this
Answer
A. 1200 10
![](https://sp-ao.shortpixel.ai/client/to_webp,q_glossy,ret_img,w_995,h_258/https://codewindow.in/wp-content/uploads/2022/05/WhatsApp-Image-2022-04-30-at-3.29.47-PM-1.jpeg)
Question 9
Answer
Question 9
What is Aggregation in OOPS?
A. One-way Association(one exists if another exists)
B. Relation between two or more objects
C. Specialized abstraction
D. None of the above
Answer
A. One-way Association(one exists if another exists)
Also Checkout: TCS NQT Sample Question Paper
public class Plant {
private String name;
public Plant(String name) {this.name = name;}
public String getname(){return name;}
}
public class Tree extends Plant {
public void growFruit() { }
public void dropLeaves() { }
}
Question 10
Answer
Question 10
Which statement is true for the above code?
A. The code will compile without changes.
B. The code will compile if public Plant() { Tree();} is added to the Plant class.
C. The code will compile if public Plant() {this(“fern”);} is added to the Plant class.
D. The code will compile if public Tree() { Plant();} is added to the Tree class.
Answer
A. The code will compile without changes.
private String thing;
String getThing() {
return thing;
}
Question 11
Answer
Question 11
Question: Based on the sample code above, what do you add to the declaration of the getThing() method for the method to be callable within a non-subclass class in a different package?
A. protected
B. private
C. public
D. transient
E. super
Answer
C. public
Question 12
Answer
Question 12
Which one of the following statements is false
A. Breadth first search cannot be used to find connected components of a graph
B. Depth first search can be used to find connected components of a graph
C. Optimal binary search tree construction can be performed efficiently using dynamic programming
D. Given the prefix and postfix walks over a binary tree, the binary tree cannot be uniquely constructed
Answer
A. Breadth first search cannot be used to find connected components of a graph
Question 13
Answer
Question 13
Which one is true about Stream API:
1. Stream APIs are not meant for storing data
2. Object creation mechanism is LAZY in stream
3. With Stream API we can consume the elements only once
4. Once we get an element from Stream API, we can use it multiple time
A. 1,2 & 3
B. 1
C. 1, 2 & 4
D. 1 & 2
Answer
C. 1, 2 & 4
Question 14
Answer
Question 14
Which is the new method introduced in java 8 to iterate over a collection?
A. for(String i : StringList)
B. List.for()
C. foreach(String i : StringList)
D. StringList.forEach()
Answer
D. StringList.forEach()
public class Tester {
static void call(Long x, Long y){
System.out.print("Long x, Long y");
}
static void call (int... x){
System.out.print("int... x");
}
static void call(Number x, Number y){
System.out.print("Number x, Number y");
}
public static void main(String[] args){
int val = 3;
call(val, val);
}
}
Question 15
Answer
Question 15
What is the result of compiling and running the following code?
A. An exception is thrown at run time
B. Number x, Number y
C. Long x, Long y
D. int… x
E. Compilation error
Answer
B. Number x, Number y
![](https://sp-ao.shortpixel.ai/client/to_webp,q_glossy,ret_img,w_997,h_260/https://codewindow.in/wp-content/uploads/2022/05/WhatsApp-Image-2022-04-30-at-3.29.46-PM.jpeg)
Question 16
Answer
Question 16
What is true about Optional
1. This is new container class introduced in java 8 which wraps a single value.
2. This is a new container interface introduced in Java 8, whose implementation class would be treated as optional if using in any collection and value is not
3. Optional is very helpful, when preventing the null checks
4. Optional.default() is used to get the default value which is to be used, if actual value is not present
A. 1 & 3
B. 1 & 3 & 4
C. Only 1
D. 2 & 3 & 4
Answer
A. 1 & 3
Question 17
Answer
Question 17
If an interface is being used in Lambda expression, how many methods with definition allowed inside that interface?
A. Only one.
B. Two but those should be marked as default
C. Any number of methods unless you have not overridden those methods
D. All of them
Answer
A. Only one.
public boolean testVal(int a){
// ????
}
Question 18
Answer
Question 18
Which line of code do you insert in place of ???? in the sample code above for the function to return true, if the value of “a” is greater than 5 and false otherwise?
A. do {if (a>5) true else false};
B. return (a>5)?true:false;
C. do {return false} if(a<=5);
D. return (a>5);
E. if(a<=5) {return = false;}else {return = false;}
Answer
D. return (a>5);
Stream s = Stream.generate(()->"meow");
boolean match = s. ___________(String::isEmpty);
System.out.println(match);
Question 19
Answer
Question 19
Which of the above can fill in the blank so that the code prints out false?(choose all that apply.)
A. None of the above
B. anyMatch
C. allMatch
D. findAny
E. findFirst
F. noneMatch
Answer
C. allMatch
public void setupFrame(String example) {
JButton button = new JButton("Example");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(example);
}
});
}
Question 20
Answer
Question 20
Question: Which do you add to the declaration of the parameter named example for the sample code above to compile and run?
A. private
B. public
C. transient
D. final
E. volatile
Answer
B. public
Also Checkout