Question 1
Question 1
Write a program which will convert the decimal number input to its corresponding binary valure. In total 4 decimal inputs will be provided with one input per line. The program is required to print the corresponding binary value in one output per line. The program should read the next input from the console only after displaying the output for the previous input.
for example:
If the following input is supplied to the program
4
6
65
1025
then the output will be-
100
110
1000001
10000000001
// Solution in java
import java.util.*;
import java.lang.*;
import java.io.*;
class Codewindow
{
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
while(str != null) {
int val = Integer.parseInt(str);
System.out.println(Integer.toBinaryString(val));
str = br.readLine();
}
}
}
Question 2
Question 2
People from a group are asked to form a circle for an activity. Then this group has to be split into two halves so that each group does a different activity as part of the original one. The task is to split it into two halves with exact same count or if there odd number of people, the first half can have more people that other. Consider the arrangement to be in the form a circular linked list and split accordingly.
Example:
——–
Input
5
1 2 3 4 5
Output
1 2 3
4 5
Explanation
If list has odd number of values then first half can have more people, so first half 5,4,3 becomes group1 and 2,1 becomes group2
// Solution in Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Codewindow
{
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(br.readLine());
int[] group = new int[num];
String[] str = br.readLine().split(" ");
for(int i=0; i<num; i++)
group[i] = Integer.parseInt(str[i]);
if(num%2 == 0) {
for(int i=0; i<num/2; i++)
System.out.print(group[i] + " ");
System.out.println();
for(int i=num/2; i<num; i++)
System.out.print(group[i] + " ");
} else {
for(int i=0; i<=num/2; i++)
System.out.print(group[i] + " ");
System.out.println();
for(int i=num/2 + 1; i<num; i++)
System.out.print(group[i] + " ");
}
}
}
Question 3
Question 3
Find the number
You are given two numbers a and b
Task:
Determine the Nth number which is divisible by a and b.
Example:
Assumptions
. a = 3
. b = 2
. N = 3
Approach
You must find the Nth number which is divisible by a and b
Here the answer is 18.
Function Description:
Complete the solve function provided in the editor. This function takes the following 3 parameters and returns Nth number which is divisible by a and b.
// Solution in Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Codewindow
{
static int gcd(int a, int b)
{
while(b != 0)
{
if(a > b)
a = a - b;
else
b = b - a;
}
return a;
}
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine());
int b = Integer.parseInt(br.readLine());
int n = Integer.parseInt(br.readLine());
int lcm = (a * b) / gcd(a, b);
System.out.println(n * lcm);
}
}
Question 4
Question 4
Network of computers
You are given a computer network in form of a grph in which each computer represent the vertex and all wires connecting the computers act as edges. The computers are numbered from 1 to n and wires are numbered from 1 to m.
Each wire connect two computers a[i] and b[i]. Find the total number of ways to mark each computer with blue, green or red color such that two computers which are directly connected are marked with different colors.
Input Format:
. First line contains n and m(representing number of computers and number of edges).
. Next m lines contains computer numberrs being connected(computer numbers a[i] and b[i] being connected by wire).
Constraints:
– 1<=n<=20
– 0<=m<=(n*(n-1))/2
– 1<= a[i] <= n
– 1<= b[i] <=n
Output Format:
Find the total number of ways to mark each computer.
// Solution will be available soon
// We are very sorry for the inconvenience caused
Question 5
Question 5
Special SSDs
You are having N special SSDs, each of them work for a[i] consecutive days and don’t work for next a[i] consecutive days. Every day you will mark the SSD of your choice which is in working condition.
How many days will it take to mark at least K SSDs or more?
Constraints:
. 1<=N<=18
. 1<=K<=10^5
. 1<=a[i]<=10^5
Input Format:
3 2
1 2 3
Output Format:
10
// Solution will be available soon
// We are very sorry for the inconvenience caused
Also Checkout: TCS NQT Sample Question Paper
Question 6
Question 6
Mathematics: Hatching of eggs
There is a fish pond in your garden. On the first day, 1 fish egg hatches, and a fish is born. On each consecutive day, the number of eggs that hatch is twice that of the previous day. For a given Nth day, Print how many eggs will hatch on that day.
Input Format:
The first line contains a single integer N.
Sample input:
2 –denotes N
Constraints:
1<=N<=20
// Solution in Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Codewindow
{
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
long eggs = (long)Math.pow(2, n-1);
System.out.println(eggs);
}
}
Question 7
Question 7
Given an integer, convert all digits except the most significant digit to zeros. You are given an integer ‘N’. Write a program that will convert the digits of the integer except the most significant digit to zeroes for example, if the input is 386, the output should be 300(This is because, 3 is the most significant digit in the number, 8 and 6 are not most significant digits. So, 8 and 6 are replaced with zeroes, 3 is retained as it is).
Sample Input:
235
Sample Output:
200
// Solution in Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Codewindow
{
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String n = br.readLine();
String newNum = n.substring(0, 1);
for(int i=1; i<n.length(); i++)
newNum = newNum + '0';
System.out.println(newNum);
}
}
Question 8
Question 8
Sort and Square Even numbers
Write a program that will sort a given list of numbers,remove odd number from the list and square each of the remaining numbers in the list.
The input file will contain the following:
Line 1: Number of elements in the list
Line 2: The numbers separated by spaces
Example 1:
Input:
5
5 4 3 2 1
Output:
4 16
Example 2:
Input:
10
2 8 6 5 9 1 3 8 4 12
Output:
4 16 36 64 64 144
// Solution in Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Codewindow
{
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] str = br.readLine().split(" ");
int num[] = new int[n];
for(int i=0; i<str.length; i++)
num[i] = Integer.parseInt(str[i]);
Arrays.sort(num);
for(int i=0; i<num.length; i++)
if(num[i]%2 == 0)
System.out.print(num[i]*num[i] + " ");
}
}
Question 9
Answer
Question 9
1. Your API is currently converting all exceptions into JSON responses, and the API just leaked sensitive information about a table in your database. What steps should you take right away to address the problem?
A. Catch all exceptions, add them to the log, and then issue a generic error response. Once you are finished with that, closely monitor the logs to see if anyone is misusing the leaked information.
B. Catch all exceptions, add them to the log, and then issue a generic error response. Once you are finished with that, change the table name, prefix, and other information leaked.
C. Change the table name, prefix, and other information leaked. Once you are finished with that, catch all exceptions, add them to the log, and then issue a generic error response.
D. Catch all exceptions, and then issue a generic error response.
Answer
B. Catch all exceptions, add them to the log, and then issue a generic error response. Once you are finished with that, change the table name, prefix, and other information leaked.
Question 10
Answer
Question 10
A variation of a linked list may be a circular linked list, during which the last node within the list points to the primary of the list. One problem with this type of list is?
A. It wastes memory space since the pointer head points to the primary node and thus the list node does not got to point to the primary node.
B. It is impossible to feature a node at the top of list.
C. It’s difficult to traverse the list because the pointer of the last node is not NULL
D. All the choices.
Answer
B. It is impossible to feature a node at the top of list.
Also Checkout