Abstraction and encapsulation are fundamental principles that underlie the object oriented approach to software develoopment. What can you say
about the following two statements?
I. Abstraction allows us to focus on what something does without considering the complexities of how it works.
II. Encapsulation allows us to consider complex ideas while ignoring irrelevent detail that would confuse us.
A. Neither I nor II is correct(answer)
B. Both I and II are correct
C. Only II is correct
D. Only I is correct
D. Only I is correct
void function(struct node* head){
if(head == NULL)
return;
function(head->next);
printf("%d ", head->data);
}
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 50
B. 50 40 30 20
C. 50 40 30 20 10
D. 10 20 30 40
C. 50 40 30 20 10
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();
}
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 seconds
B. 2 seconds
C. 1.5 seconds
D. 1.4142 seconds
E. 4 seconds
E. 4 seconds
Consider the implementation of the stack using partially filled array. What goes wrong, if we try to store the top of the stack at location[0] and the bottom of the stack at the last position of the array.
A. The stack could not be used to evaluate postfix expression.
B. Both push and pop would required linear time
C. Both push and pop would require non-linear time
D. The stack could not be used to check balanced parenthesis
B. Both push and pop would required linear time
Which one of the following are essential features of object oriented language?
A. Abstraction and encapsulation
B. Strictly-typed
C. Type-safe property coupled with sub-type rule
D. Polymorphism in the presence of inheritence
A. A and B only
B. A,D, and B only
C. A and D only
D. A,C, and D only
C. A and D only
If a field is static and is a reference type, where will it go?
A. stack
B. heap
C. static storage area
D. All
C. static storage area
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.
C. Entry from WeakHashMap will never be removed by Garbage collector.
Necessity of overriding clone method comes when
A. When we have more descendents of our class
B. None of the above
C. When we want to call clone our object from different class
D. When we want the different behavior of clone method
D. When we want the different behavior of clone method
void function(struct Node *head)
{
if (head==NULL)
return;
struct Node *p1 = head;
struct Node *node = head->next;
while(p1 != NULL && node != NULL)
{
p1->next = node->next;
free(node);
p1 = p1->next;
if(p1 != NULL)
node = p1->next;
}
}
What will be the elements of given singly linked(1->2->3->4->5) with first node as head after calling function(head)?
A. 1->2->3->4->5
B. 1->3->5
C. 2->3->4->5
D. 2->4
B. 1->3->5
Also Checkout: TCS NQT Sample Question Paper
public class Wow {
public static void go(short n) {System.out.println("short");}
public static void go(Short n) {System.out.println("SHORT");}
public static void go(Long n) {System.out.println("LONG");}
public static void main(String [] args){
Short y = 6;
int z = 7;
go(y);
go(z);
}
}
What will be the output of the code snippet?
A. SHORT LONG
B. An exception is thrown at runtime.
C. short LONG
D. Compilation fails.
D. Compilation fails.
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() { }
}
Which statement is true for the following code?
A. The code will compile if public Plant() {this(“fern”);} is added to the Plant class.
B. The code will compile without changes.
C. The code will compile if public Plant() { Tree();} is added to the Plant class.
D. The code will compile if public Tree() { Plant();} is added to the Tree class.
B. The code will compile without changes.
import java.util.List;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
public class test
{
public static List<Integer> generate(int series){
return Stream.iterate(new int[]{0,1}, s->new int[]{s[1],s[0]+s[1]})
.limit(series)
.map(n -> n[0])
.collect(toList());
}
public static void main(String [] args) {
System.out.println(test.generate(5));
}
}
Find the output for the above code?
A. error in .collect(toList());
B. [0,1,1,2,3]
C. [1,2,3,2,1]
D. compilation fail
A. error in .collect(toList());
public interface FooInterface{
default public void defaultMethod(){
System.out.println("FooInterface");
}
}
public interface BarInterface{
default public void defaultMethod(){
System.out.println("BarInterface");
}
}
public class FooBarChild implements FooInterface, BarInterface {
public static void main(String[] args){
FooInterface foo = new FooBarChild();
foo.defaultMethod();
}
public void defaultMethod(){
System.out.println("Child's Own");
}
}
What will be output of the sample code given:
A. Child’s Own
B. FooInterface
C. Foo Interface Child’s own
D. None of these
A. Child’s Own
public class testmeth
{
static int i = 1;
public static void main(String args[]){
System.out.println(i+",");
m(i);
System.out.println(i);
}
public void m(int i){
i += 2;
}
}
What is the output of the above program:
A. 1,3
B. 3,1
C. 1,1
D. 1,0
A. 1,3
Scenario: You want to manage the cookies that are sent and received for a series of successive network connections during a session-oriented web connection.
Question: Based on the scenario above, which code sequence do you use to install the CookieHandler for a specific URL connection?
A. String urlString=…
Toolkit.getDefaultToolkit().setDefaultCookie(new.CookieManager());
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
B. String urlString=…
CookieHandler.setDefault(new CookieManager());
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
C. String urlString=…
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.setCookieHandler(new CookieManager());
D. String urlString=…
System.setDefaultCookieHandler(new CookieManager());
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
E. String urlString=…
URL url = new URL(urlString);
url.setCookie(new CookieManager());
URLConnection connection = url.openConnection();
D. String urlString=…
System.setDefaultCookieHandler(new CookieManager());
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
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);
}
}
What is the result of compiling and running the following code?
A. int… x
B. Compilation error
C. An exception is thrown at run time
D. Long x, Long y
E. Number x, Number y
E. Number x, Number y
import java.util.*;
import java.lang.*;
import java.io.*;
public class d
{
public static void main(String []args) throws java.lang.Exception
{
int i1 = 2;
int i2 = 5;
double d;
d = 3 + i1/i2 +2;
System.out.println(d);
}
}
What is the value of d after the sample code above is executed?
A. 3.0
B. 7.5
C. 5.0
D. 5.2
E. 5.4
C. 5.0
abstract class AirPlane{
abstract void fly();
void land(){ // line 5
System.out.print("Landing..");
}
}
class AirJet extends AirPlane{
AirJet() {
super(); // line 14
}
void fly() {
System.out.print("Flying..");
}
}
Will the above code compile correctly?
A. No, because class AirJet must override method land()
B. No, because at line 5 method land() must be abstract since class AirPlane is abstract
C. Yes, it will compile with no errors
D. “No, because at line 14 AirJet constructor is calling the super() while AirPlane has no constructor defined”
C. Yes, it will compile with no errors
String[] arr = new String[5];
int[] arr1 = new int[5];
Scanner kb = new Scanner (System.in);
for(int i = 0; i<5; i++)
{
System.out.println("Enter text: ");
arr[i] = kb.nextLine();
System.out.println("Enter number: ");
arr1[i] = kb.nextInt();
}
Question: Based on the sample code above, which change do you make to alternately add entered strings into arr and entered numbers into arr1?
A. Change Line 2 String[] arr1 = new String[5];
B. Remove Line 1 from the code
C. Add kb.nextLine(); after Line 9
D. Add Scanner kb = new Scanner(System.in); after Line 10
E. Remove Line 9 from the code
C. Add kb.nextLine(); after Line 9
Which of the following represents preorder traversal of the given tree
A
/ \
B F
\ / \
E D H
/ / \
C I G
A. A B D E F C G H I
B. D B A F E G C I H
C. A B E F D C H I G
D. D B E F A G C H I
C. A B E F D C H I G
Also Checkout