C Programming Interview Questions Guide
C Programming Interview Questions Guide
THAT ARE CONTAINING ALL BASICS AND THOSE ARE REQUIRED FOR
INTERVIEW THIS MIGHT BE HELPFUL IN MANY WAYS TO CRACK
CODING TEST OR TECHNICAL ROUND INTERVIEW . ~K V S NAIDU
1 | C-PROGRAMMING BY K V S N
C program with numbers
3 | C-PROGRAMMING BY K V S N
12. Write a c program to convert binary number to hexadecimal
number.
13. C program for addition of binary numbers .
14. C program for multiplication of two binary numbers.
15. C program fractional binary conversion from decimal.
16. C program for fractional decimal to binary fraction
conversion.
17. C program to convert decimal number to roman.
18. C program to convert roman number to decimal number.
19. C program to convert each digits of a number in words
20. C program to convert currency or number in word.
Conversion ( Unit )
1. C program for unit conversion.
String
1. Write a c program to convert the string from upper case to
lower case.
2. Write a c program to convert the string from lower case to
upper case.
3. Write a c program to delete the all consonants from given
string.
4. Write a c program to count the different types of characters in
given string.
5. Write a c program to sort the characters of a string.
6. Write a c program for concatenation two strings without using
string.h header file.
7. Write a c program to find the length of a string using pointer.
8. Write a c program which prints initial of any name.
4 | C-PROGRAMMING BY K V S N
9. Write a c program to print the string from given character.
10. Write a c program to reverse a string
11. Reverse a string using recursion in c
12. String concatenation in c without using strcat
13. How to compare two strings in c without using strcmp
14. String copy without using strcpy in c
15. Convert a string to ASCII in c
Matrix
1. Write a c program for addition of two matrices.
2. Write a c program for subtraction of two matrices
3. Write a c program for multiplication of two matrices.
4. Write a c program to find out sum of diagonal element of a
matrix.
5. Write a c program to find out transport of a matrix.
6. Write a c program for scalar multiplication of matrix.
7. C program to find inverse of a matrix
8. Lower triangular matrix in c
9. Upper triangular matrix in c
10. Strassen's matrix multiplication program in c
11. C program to find determinant of a matrix
File
1. Write a c program to open a file and write some text and close
its.
2. Write a c program to delete a file.
3. Write a c program to copy a file from one location to other
location.
4. Write a c program to copy a data of file to other file.
5. Write a c program which display source code as a output.
5 | C-PROGRAMMING BY K V S N
6. Write a c program which writes string in the file.
7. Write a c program which reads string from file.
8. Write a c program which writes array in the file.
9. Write a c program which concatenate two file and write it
third file.
10. Write a c program to find out size of any file.
11. Write a c program to know type of file.
12. Write a c program to know permission of any file.
13. Write a c program to know last date of modification of any
file.
14. Write a c program to find size and drive of any file.
Complex number
7 | C-PROGRAMMING BY K V S N
2. Write a c program to find GCD of a two numbers using
recursion.
3. Write a c program to find out sum digits of a number using
recursion.
4. Write a c program to find power of a number using function
recursion.
5. Write a c program to reverse any number using recursion.
Size of data type
1. Write a c program to find the size of int without using sizeof
operator.
2. Write a c program to find the size of double without using
sizeof operator.
3. Write a c program to find the size of structure without using
sizeof operator.
4. Write a c program to find the size of union without using
sizeof operator.
Using pointer
1. Write a c program for concatenation two string using pointer.
Searching
1. Write a c program for linear search.
2. Write a c program for binary search.
3. Write a c program for binary search using recursion.
Area and volume
1. Write a c program to find the area of circle.
8 | C-PROGRAMMING BY K V S N
2. Write a c program to find the area of any triangle.
3. Write a c program to find the area of equilateral triangle.
4. Write a c program to find the area of right angled triangle.
5. Write a c program to find the area of rectangle.
6. Write a c program to find the area of trapezium.
7. Write a c program to find the area of rhombus.
8. Write a c program to find the area of parallelogram.
9. Write a c program to find the volume and surface area of
cube.
10. Write a c program to find the volume and surface area of
cuboids.
11. Write a c program to find the volume and surface area
of cylinder.
12. Write a c program to find the surface area and volume of a
cone.
13. Write a c program to find the volume and surface area of
sphere.
14. Write a c program to find the perimeter of a circle, rectangle
and triangle.
10 | C-PROGRAMMING BY K V S N
///////**********************PROGRAMS BY K V SUBBA NAIDU*********************////////////
PERFECT NUMBER
Code 1:
1. C program to check perfect number
#include<stdio.h>
int main(){
int n,i=1,sum=0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}
11 | C-PROGRAMMING BY K V S N
Code 2:
#include<stdio.h>
int main(){
int n,i,sum;
int min,max;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n);
}
return 0;
}
12 | C-PROGRAMMING BY K V S N
Code 3:
3. C program to print perfect numbers from 1 to 100
#include<stdio.h>
int main(){
int n,i,sum;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n);
}
return 0;
}
Output:
Perfect numbers are: 6 28
13 | C-PROGRAMMING BY K V S N
*************************************************************************************
ARMSTRONG NUMBER
14 | C-PROGRAMMING BY K V S N
Code 1:
#include<stdio.h>
int main(){
int num,r,sum=0,temp;
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}
15 | C-PROGRAMMING BY K V S N
Code 2:
#include<stdio.h>
int main(){
int num,r,sum,temp;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
return 0;
}
16 | C-PROGRAMMING BY K V S N
Code 3:
#include<stdio.h>
int main(){
int num,r,sum=0,temp;
for(temp=num;num!=0;num=num/10){
r=num%10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}
17 | C-PROGRAMMING BY K V S N
Code 4:
#include<stdio.h>
int main(){
int num,r,sum,temp;
for(num=1;num<=500;num++){
temp=num;
sum = 0;
while(temp!=0){
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
return 0;
}
18 | C-PROGRAMMING BY K V S N
*************************************************************************************
19 | C-PROGRAMMING BY K V S N
Code 1:
#include<stdio.h>
int main(){
int num,i,count=0;
printf("Enter a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}
Sample output:
Enter a number: 5
5 is a prime number
20 | C-PROGRAMMING BY K V S N
Code 2:
#include<stdio.h>
int main(){
int num,i,count;
for(num = 1;num<=100;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
return 0;
}
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
21 | C-PROGRAMMING BY K V S N
Code 3:
#include<stdio.h>
int main(){
int num,i,count,n;
printf("Enter max range: ");
scanf("%d",&n);
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
return 0;
}
Sample output:
Enter max range: 50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
22 | C-PROGRAMMING BY K V S N
Code 4:
#include<stdio.h>
int main(){
int num,i,count,min,max;
num = min;
while(num<=max){
count = 0;
i=2;
while(i<=num/2){
if(num%i==0){
count++;
break;
}
i++;
}
num++;
}
return 0;
}
Sample output:
Enter min range: 50
Enter max range: 100
53 59 61 67 71 73 79 83 89 97
23 | C-PROGRAMMING BY K V S N
Code 5:
#include<stdio.h>
int main(){
int num,i,count,min,max;
for(num = min;num<=max;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
return 0;
}
Sample output:
Enter min range: 10
Enter max range: 50
11 13 17 19 23 29 31 37 41 43 47
24 | C-PROGRAMMING BY K V S N
Code 6:
#include<stdio.h>
int main(){
int num,i,count,sum=0;
for(num = 1;num<=100;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
return 0;
}
Output:
Sum of prime numbers is: 1060
25 | C-PROGRAMMING BY K V S N
Code 7:
#include<stdio.h>
int main(){
int num,i,count,min,max,sum=0;
for(num = min;num<=max;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
return 0;
}
26 | C-PROGRAMMING BY K V S N
*******************************************************
STRONG NUMBER OR NOT
Code 1:
#include<stdio.h>
int main(){
int num,i,f,r,sum=0,temp;
temp=num;
while(num){
i=1,f=1;
r=num%10;
while(i<=r){
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
if(sum==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);
return 0;
}
27 | C-PROGRAMMING BY K V S N
Code 2:
#include<stdio.h>
int main(){
int num,i,f,r,sum,temp;
int min,max;
while(temp){
i=1;
f=1;
r=temp%10;
while(i<=r){
f=f*i;
i++;
}
sum=sum+f;
temp=temp/10;
}
if(sum==num)
printf("%d ",num);
}
return 0;
}
Sample output:
Enter minimum range: 100
Enter maximum range: 100000
Strong numbers in given range are: 145 40585
28 | C-PROGRAMMING BY K V S N
******************************************************************************
EVEN OR ODD NUMBER
Code 1:
#include<stdio.h>
int main(){
int number;
if(number % 2 ==0)
printf("%d is even number.",number);
else
printf("%d is odd number.",number);
return 0;
Sample output:
Enter any integer: 5
5 is odd number.
29 | C-PROGRAMMING BY K V S N
Code 2:
#include<stdio.h>
int main(){
int number;
int min,max;
if(number % 2 !=0)
printf("%d ",number);
return 0;
Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
Odd numbers in given ranges are: 1 3 5 7 9 11 13 15 17
19
30 | C-PROGRAMMING BY K V S N
Code 3:
#include<stdio.h>
int main(){
int number;
int min,max;
if(number % 2 !=0)
printf("%d ",number);
if(number % 2 ==0)
printf("%d ",number);
return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
Odd numbers in given ranges are: 1 3 5 7 9 11 13 15 17
19
Even numbers in given ranges are: 2 4 6 8 10 12 14 16
18 20
31 | C-PROGRAMMING BY K V S N
Code 4:
#include<stdio.h>
int main(){
int number;
int min,max;
long sum =0;
return 0;
Sample output:
Enter the minimum range: 1
Enter the maximum range: 100
Sum of odd numbers in given range is: 2500
32 | C-PROGRAMMING BY K V S N
Code 5:
#include<stdio.h>
int main(){
int number;
int min,max;
long odd_sum =0,even_sum = 0;
return 0;
Sample output:
Enter the minimum range: 1
Enter the maximum range: 10
Sum of even numbers in given range is: 30
Sum of odd numbers in given range is: 25
33 | C-PROGRAMMING BY K V S N
******************************************************************************
PALINDROME OR NOT (NUMBER)
Code 1:
#include<stdio.h>
int main(){
int num,r,sum=0,temp;
temp=num;
while(num){
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("%d is a palindrome",temp);
else
printf("%d is not a palindrome",temp);
return 0;
}
Sample output:
Enter a number: 131
131 is a palindrome
34 | C-PROGRAMMING BY K V S N
Code 2:
#include<stdio.h>
int main(){
int num,r,sum,temp;
int min,max;
while(temp){
r=temp%10;
temp=temp/10;
sum=sum*10+r;
}
if(num==sum)
printf("%d ",num);
}
return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 50
Palindrome numbers in given range are: 1 2 3 4 5 6 7 8
9 11 22 33 44
35 | C-PROGRAMMING BY K V S N
Code 3:
#include<stdio.h>
int main(){
int num,r,sum=0,temp;
for(temp=num;num!=0;num=num/10){
r=num%10;
sum=sum*10+r;
}
if(temp==sum)
printf("%d is a palindrome",temp);
else
printf("%d is not a palindrome",temp);
return 0;
}
Sample output:
Enter a number: 1221
1221 is a palindrome
36 | C-PROGRAMMING BY K V S N
Code 4:
#include<stdio.h>
int checkPalindrome(int);
int main(){
int num,sum;
sum = checkPalindrome(num);
if(num==sum)
printf("%d is a palindrome",num);
else
printf("%d is not a palindrome",num);
return 0;
}
if(num!=0){
r=num%10;
sum=sum*10+r;
checkPalindrome(num/10);
}
return sum;
}
Sample output:
Enter a number: 25
25 is not a palindrome
37 | C-PROGRAMMING BY K V S N
******************************************************************************
PALINDROME OR NOT (NUMBER)
Code 1
#include<string.h>
#include<stdio.h>
int main(){
char *str,*rev;
int i,j;
printf("\nEnter a string:");
scanf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nThe string is not a palindrome");
else
printf("\nThe string is a palindrome");
return 0;
}
******************************************************************************
// TO BE WRITTEN
Code 1
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
38 | C-PROGRAMMING BY K V S N
float d,root1,root2;
d = b * b - 4 * a * c;
return 0;
}
else if(d==0){
printf("Both roots are equal.\n");
return 0;
}
else{
printf("Roots are real numbers.\n");
return 0;
}
Sample output:
Enter a, b and c of quadratic equation: 2 4 1
Roots are real numbers.
Roots of quadratic equation are: -0.293, -1.707
39 | C-PROGRAMMING BY K V S N
Code 2
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
float d,root1,root2;
d = b * b - 4 * a * c;
return 0;
}
return 0;
}
Sample output:
Enter quadratic equation in the format ax^2+bx+c:
2x^2+4x+-1
Roots of quadratic equation are: 0.000, -2.000
40 | C-PROGRAMMING BY K V S N
******************************************************************************
FIBONACCI SERIES
Fn = Fn-2 + Fn-1
Code 1:
#include<stdio.h>
int main(){
int k,r;
long int i=0l,j=1,f;
for(k=2;k<r;k++){
f=i+j;
i=j;
j=f;
printf(" %ld",j);
}
return 0;
}
41 | C-PROGRAMMING BY K V S N
Code 2:
1. Fibonacci series using array in c
2. Fibonacci series program in c language
3. Source code of Fibonacci series in c
4. Wap to print Fibonacci series in c
#include<stdio.h>
int main(){
int i,range;
long int arr[40];
arr[0]=0;
arr[1]=1;
for(i=2;i<range;i++){
arr[i] = arr[i-1] + arr[i-2];
}
return 0;
}
Sample output:
Enter the number range: 20
Fibonacci series is: 0 1 1 2 3 5 8 13 21 34 55 89 144
233 377 610 987 1597 2584 4181
42 | C-PROGRAMMING BY K V S N
Code 3:
#include<stdio.h>
int main(){
int k=2,r;
long int i=0l,j=1,f;
while(k<r){
f=i+j;
i=j;
j=f;
printf(" %ld",j);
k++;
}
return 0;
}
Sample output:
Enter the number range: 10
Fibonacci series is: 0 1 1 2 3 5 8 13 21 34
43 | C-PROGRAMMING BY K V S N
Code 4:
#include<stdio.h>
int main(){
int k,r;
long int i=0,j=1,f;
long int sum = 1;
for(k=2;k<r;k++){
f=i+j;
i=j;
j=f;
sum = sum + j;
}
return 0;
}
Sample output:
Enter the number range: 4
Sum of Fibonacci series is: 4
44 | C-PROGRAMMING BY K V S N
******************************************************************************
FACTORIAL OF A NUMBER
Definition / Algoritham
Code 1:
#include<stdio.h>
int main(){
int i=1,f=1,num;
while(i<=num){
f=f*i;
i++;
}
Sample output:
Enter a number: 5
Factorial of 5 is: 120
45 | C-PROGRAMMING BY K V S N
Code 2:
#include<stdio.h>
int main(){
int i,f=1,num;
for(i=1;i<=num;i++)
f=f*i;
Code 3:
#include<stdio.h>
findFactorial(num,&factorial);
printf("Factorial of %d is: %d",num,*factorial);
return 0;
}
*factorial =1;
for(i=1;i<=num;i++)
*factorial=*factorial*i;
}
46 | C-PROGRAMMING BY K V S N
Code 4:
1. Factorial program in c using function
2. C program to find factorial of a number
#include<stdio.h>
int findFactorial(int);
int main(){
int i,factorial,num;
factorial = findFactorial(num);
printf("Factorial of %d is: %d",num,factorial);
return 0;
}
for(i=1;i<=num;i++)
f=f*i;
return f;
}
Sample output:
Enter a number: 8
Factorial of 8 is: 40320
47 | C-PROGRAMMING BY K V S N
Code 5:
1. Factorial series in c
#include<stdio.h>
int main(){
long f=1;
int i,num,min,max;
for(i=1;i<=num;i++)
f=f*i;
printf("%ld ",f);
}
return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 10
Factorial series in given range: 1 2 6 24 120 720 5040
40320 362880 3628800
48 | C-PROGRAMMING BY K V S N
******************************************************************************
FLOYD’S TRIANGLE
Example 1:
1
2 3
4 5 6
7 8 9 10
Example 2:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
49 | C-PROGRAMMING BY K V S N
1. Write a c program to print Floyd’s triangle
#include<stdio.h>
int main(){
int i,j,r,k=1;
printf("FLOYD'S TRIANGLE\n\n");
for(i=1;i<=r;i++){
for(j=1;j<=i;j++,k++)
printf(" %d",k);
printf("\n");
}
return 0;
}
Sample output:
Enter the range: 10
FLOYD'S TRIANGLE
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
50 | C-PROGRAMMING BY K V S N
******************************************************************************
PASCAL’S TRIANGLE
51 | C-PROGRAMMING BY K V S N
#include<stdio.h>
long fact(int);
int main(){
int line,i,j;
for(i=0;i<line;i++){
for(j=0;j<line-i-1;j++)
printf(" ");
for(j=0;j<=i;j++)
printf("%ld ",fact(i)/(fact(j)*fact(i-
j)));
printf("\n");
}
return 0;
}
52 | C-PROGRAMMING BY K V S N
******************************************************************************
MULTIPLICATION TABLE
// WAITLISTED
#include<stdio.h>
int main(){
int r,i,j,k;
printf("Enter the number range: ");
scanf("%d",&r);
for(i=1;i<=r;i++){
for(j=1;j<=10;j++)
printf("%d*%d=%d ",i,j,i*j);
printf("\n");
}
return 0;
}
Sample Output:
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1*10=10
2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*10=20
3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 3*10=30
4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36
4*10=40
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45
5*10=50
53 | C-PROGRAMMING BY K V S N
******************************************************************************
PRINTING ASCII VALUES’ USING C-PROGRAM
#include<stdio.h>
int main(){
int i;
for(i=0;i<=255;i++)
printf("ASCII value of character %c:
%d\n",i,i);
return 0;
}
54 | C-PROGRAMMING BY K V S N
******************************************************************************
print hello world without using semicolon
LOGICS TO PRINT HELLO WORLD
#include<stdio.h>
void main(){
if(printf("Hello world")){
}
}
Solution: 2
#include<stdio.h>
void main(){
while(!printf("Hello world")){
}
}
Solution: 3
#include<stdio.h>
void main(){
switch(printf("Hello world")){
}
}
55 | C-PROGRAMMING BY K V S N
******************************************************************************
own source code as its output
#include<stdio.h>
int main(){
FILE *fp;
char c;
fp = fopen(__FILE__,"r");
do{
c= getc(fp);
putchar(c);
}
while(c!=EOF);
fclose(fp);
return 0;
}
Output:
#include<stdio.h>
int main(){
FILE *fp;
char c;
fp = fopen(__FILE__,"r");
do{
c= getc(fp);
putchar(c);
}
while(c!=EOF);
fclose(fp);
return 0;
}
56 | C-PROGRAMMING BY K V S N
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
57 | C-PROGRAMMING BY K V S N
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
58 | C-PROGRAMMING BY K V S N
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
59 | C-PROGRAMMING BY K V S N
60 | C-PROGRAMMING BY K V S N
61 | C-PROGRAMMING BY K V S N
Destination Technologies – TCS Ninja Training (Coding Round)
Bangalore 9888738888
Hyderabad 9888748888
Vijayawada 9888758888
Program:
Write a java program to print the following output
class Demo
for(int j =1;j<=5;j++)
for(int i=1;i<=5;i++ )
System.out.print("*");
else
System.out.print(" ");
System.out.println();
Program:
Write a java program to print the following output
class Demo
for(int j =1;j<=10;j++)
for(int i=1;i<=10;i++ )
System.out.print("*");
else
System.out.print(" ");
System.out.println();
Program:
Write a java program to print the following output
class Demo
for(int j =1;j<=10;j++)
for(int i=1;i<=10;i++ )
System.out.print("*");
else
System.out.print(" ");
System.out.println();
Bangalore 9888738888
Hyderabad 9888748888
Vijayawada 9888758888
Program 9:
Write a java program to print the following output
class Demo
for(int j =1;j<=5;j++)
System.out.print(" ");
for(int i=1;i<=5;i++ )
System.out.print ("*");
System.out.println();
Program 10:
Write a java program to print the following output
class Demo
for(int j =1;j<=5;j++)
System.out.print(" ");
for(int i=1;i<=5;i++ )
System.out.print ("*");
System.out.println();
Program 11:
Write a java program to print the following output
class Demo
for(int j =1;j<=5;j++)
for(int i=1;i<=j;i++ )
System.out.print ("*");
System.out.println();
Program 12:
Write a java program to print the following output
class Demo
for(int j =1;j<=5;j++)
for(int i=1;i<=j;i++ )
System.out.print(i);
System.out.println();
Program 13:
Write a java program to print the following output
class Demo
for(int j =1;j<=5;j++)
for(int i=1;i<=j;i++ )
System.out.print(j);
System.out.println();
Program 14:
Write a java program to print the following output
class Demo
for(int j =1;j<=5;j++)
for(int i=5;i>=j;i-- )
System.out.print(i);
System.out.println();
Program 15:
Write a java program to print the following output
class Demo
for(int j =1;j<=5;j++)
for(int i=5;i>=j;i-- )
System.out.print(i);
System.out.println();
Program 16:
Write a java program to print the following output
class Demo
for(int j =1;j<=5;j++)
for(int i=5;i>=j;i-- )
System.out.print(j);
System.out.println();
Program 17:
Write a java program to print the following output
class Demo
for(int j =1;j<=5;j++)
for(int k=5;k>=j;k-- )
System.out.print(" ");
for(int i=1;i<=j;i++ )
System.out.print("*");
System.out.println();
Program 18:
Write a java program to print the following output
class Demo
for(int j =1;j<=5;j++)
for(int k=1;k<=j;k++ )
System.out.print(" ");
for(int i=5;i>=j;i-- )
System.out.print("*");
System.out.println();
Program 1:
Write a java program to print the following output
class Demo
System.out.println("*");
Program 2:
Write a java program to print the following output
class Demo
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
Bangalore Hyderabad Vijayawada
9888738888 9888748888 9888758888
www.destinationtechs.com || [email protected]
Destination Technologies – TCS Ninja Training (Coding Round)
}
The above style of writing the code is not suggested as already explained in the lecture.
Program 3:
Write a java program to print the following output using for loop
class Demo
for(int i=1;i<=5;i++)
System.out.println("*");
Program 4:
Write a java program to print the following output using for loop
class Demo
for(int i=1;i<=5;i++ )
System.out.print ("*");
Program 5:
Write a program for the following output without using loops
class Demo
for(int i=1;i<=5;i++ )
System.out.print ("*");
for(int i=1;i<=5;i++ )
System.out.print ("*");
for(int i=1;i<=5;i++ )
System.out.print ("*");
for(int i=1;i<=5;i++ )
System.out.print ("*");
for(int i=1;i<=5;i++ )
System.out.print ("*");
The above style of writing the code is not suggested as already explained in the lecture.
Program 6:
Write a program for the following output using loops
class Demo
for(int j =1;j<=5;j++)
for(int i=1;i<=5;i++ )
System.out.print ("*");
Program 7:
Write a program for the following output using loops
class Demo
for(int j =1;j<=5;j++)
for(int i=1;i<=5;i++ )
System.out.print ("*");
System.out.println();
Program 8:
Write a program for the following output using loops
class Demo
for(int j =1;j<=5;j++)
for(int i=1;i<=3;i++ )
System.out.print ("*");
System.out.println();
}
}
Bangalore Hyderabad Vijayawada
9888738888 9888748888 9888758888
www.destinationtechs.com || [email protected]
Telegram - https://t.me/campusdrive
This series is a mixture of 2 series - all the odd terms in this series form a Fibonacci series and all the
even terms are the prime numbers in ascending order.
The value N is a Positive integer that should be read from STDIN. The Nth term that is calculated by
the program should be written to STDOUT. Other than the value of Nth term, no other
characters/strings or message should be written to STDOUT.
For example, when N = 14, the 14th term in the series is 17. So only the value 17 should be printed to
STDOUT.
#include<iostream>
using namespace std;
#define MAX 1000
void fibonacci(int n)
{
int i, t1 = 0, t2 = 1, nextTerm;
for (i = 1; i<=n; i++)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
cout << t1;
}
void prime(int n)
{
int i, j, flag, count =0;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
2. Given a series whose even term creates a separate geometric series and odd term creates another
geometric series.
#include<iostream>
using namespace std;
int main()
{
int n, i, r1, r2;
cout << "nEnter the total number of terms : ";
cin >> n;
cout << "nEnter the common ratio for GP - 1 : ";
cin >> r1;
cout << "nEnter the common ratio for GP - 2 : ";
cin >> r2;
cout << "nThe series isn";
int a = 1, b = 1;
if(n % 2 == 0)
{
for(i = 0; i < n/2; i++)
{
cout << a << " ";
a = a * r1;
cout << b << " ";
b = b * r2;
}
}
else
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
{
for(i = 0; i < n/2; i++)
{
cout << a << " ";
a = a * r1;
cout << b << " ";
b = b * r2;;
}
cout << a << " ";
}
cout << endl;
}
Program to find the area of a circle is discussed here. Area of the circle can be found using the
formula, A = πr2 where r is the radius of the circle. When the radius of the circle is known, the area of
the circle can be calculated using the formula mentioned.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
#include <iostream>
int main()
int year;
if(year%4 == 0)
if( year%100 == 0)
if ( year%400 == 0)
else
else
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
else
return 0;
5. GCD of two numbers | Program to find the GCD or HCF of two numbers
#include<iostream>
int main()
int a,b,gcd;
int i;
gcd = i;
cout << “\nGCD of “<< a << ” and ” << b << ” is ” << gcd;
return 0;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
#include<stdio.h>
int main()
int n,i;
scanf(“%d”,&n);
if(n % i ==0)
break;
else
return 0;
#include <iostream>
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
int main()
int a, b, i, flag;
cin >> a;
cin >> b;
cout << “\nPrime Numbers between ” << a << ” and ” << b <<” : “;
while (a < b)
flag = 0;
if(a % i == 0)
flag = 1;
break;
if (flag == 0)
++a;
return 0;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
#include<iostream>
int main()
int n,i;
int fact,rem;
cin >> n;
int sum = 0;
int temp = n;
while(n)
i = 1,fact = 1;
rem = n % 10;
fact = fact * i;
i++;
n = n / 10;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
if(sum == temp)
else
return 0;
#include<iostream>
int is_Palindrome(int );
int n;
int main()
int palindrome;
cin >> n;
palindrome = is_Palindrome(n);
if(palindrome == 1)
else
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
return 0;
if(aj != 0)
else if(sum == n)
return 1;
else
return 0;
#include<iostream>
int main()
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
temp = number;
while (temp != 0)
temp /= 10;
++n;
temp = number;
while (temp != 0)
remainder = temp ;
temp /= 10;
if(result == number)
else
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
return 0;
// C++ program to print the Armstrong numbers between the two intervals
#include <iostream>
#include <math.h>
int main()
cout << “\nArmstrong numbers between ” << start << ” and ” << end << ” are : “;
temp2 = i;
temp1 = i;
while (temp1 != 0)
temp1 /= 10;
++n;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
while (temp2 != 0)
temp2 /= 10;
if (result == i) {
n = 0;
result = 0;
return 0;
#include<iostream>
int main()
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
int sum = 0, n;
int a = 0;
int b = 1;
cin >> n;
while(sum <= n)
a = b; // swap elements
b = sum;
return 0;
#include
while (n!=0)
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
remainder = n ;
n /= 10;
decimal += remainder*pow(2,i);
++i;
return decimal;
int main()
long int n;
cin >> n;
return 0;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
#include <iostream>
int main()
int a, b, i, flag;
cin >> a;
cin >> b;
cout << “\nPrime Numbers between ” << a << ” and ” << b <<” : “;
while (a < b)
flag = 0;
if(a % i == 0)
flag = 1;
break;
if (flag == 0)
++a;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
return 0;
#include <iostream>
int main()
cin >> n;
while(n != 0)
rem = n ;
n /= 10;
return 0;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
#include <iostream>
int main()
int i, j, count = 0;
count++;
j = count - 1;
rev[i] = str[j];
j--;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
**
***
****
*****
******
#include <iostream>
int main()
int i, j,n;
cin >> n;
return 0;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
#include <iostream>
int main()
int n, c, k, space = 1;
cin >> n;
space = n – 1;
cout << ” “;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
space--;
space = 1;
cout << ” “;
space++;
return 0;
#include <bits/stdc++.h>
int main()
int n,i;
cin >> n;
int arr[n];
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
sort(arr, arr+n);
return 0;
#include<iostream>
if (n==0 || n==1)
return n;
int temp[n];
int j = 0;
int i;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
if (arr[i] != arr[i+1])
temp[j++] = arr[i];
temp[j++] = arr[n-1];
arr[i] = temp[i];
return j;
int main()
int n;
cin >> n;
int arr[n];
int i;
n = remove_duplicate_elements(arr, n);
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
return 0;
Telegram - https://t.me/tcsnqtexam
Swapping 2 numbers
Type 1 :
class Demo
int a,b,temp;
a=10;
b=20;
temp=a;
a=b;
b=temp;
Type 2:
class Demo
int a,b,temp;
a=10;
b=20;
a=a+b;
b=a-b;
a=a-b;
Type 3:
class Demo
int a,b,temp;
a=10;
b=20;
a=a*b;
b=a/b;
a=a/b;
Type 4:
class Demo
int a,b,temp;
a=10;
b=20;
a=a^b;
b=a^b;
a=a-^b;
}
Coding Question-1
Considerthe peiow series:
1,2,1,3,2,5,3,7,5,11,8,13,13,17,
This series is a mixture of 2 series fail the odd terms in this series form a
Fibonacci series and all the even terms are the prime numbers in ascending
order
The value N in a positive integer that should be read from mm. The Nth term
that is calculated by the program should be written to STDOUT Otherthan the
value of Nth term , no other characters / string or message should be written to
STDOUT.
For example, when N:14, the 14th term in the series is 17 So only the value 17
should be printed to STDOUT
Solution –
#include
void fibo(int);
void prime(int);
main()
int n,e;
scanf("%d",&n);
e=n/2;
if(n%2==0)
prime(e);
else
fibo(e+1);
}
void prime(int n)
int i,j,no,flag=0,count=0;
for(i=1;i<=100;i++)
flag=0;
for(j=2;j<=i/2;j++)
if(i%j==0)
flag=0;
else
flag=1;
if(flag==1)
count++;
if(count==n)
printf("%d\n",i);
break;
}
}
void fibo(int n)
int n0=0,n1=1,n2,i;
for(i=3;i<=n;i++)
n2=n0+n1;
n0=n1;
n1=n2;
printf("%d",n2);
--------------------------------------------------------------------------------------------------------
Coding Question-2
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-
place comparison-based algorithm in which the list is divided into two parts,
the sorted part at the left end and the unsorted part at the right end. Initially,
the sorted part is empty and the unsorted part is the entire list.
The smallest element is selected from the unsorted array and swapped with
the leftmost element, and that element becomes a part of the sorted array.
This process continues moving unsorted array boundary by one element to
the right.
This algorithm is not suitable for large data sets as its average and worst case
complexities are of Ο(n2), where n is the number of items.’
For the first position in the sorted list, the whole list is scanned sequentially.
The first position where 14 is stored presently, we search the whole list and
find that 10 is the lowest value.
Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus
leo.
Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus
leo.
Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus
leo.
The same process is applied to the rest of the items in the array.
In Python
In Java
Bubble Sort
Sorting Algorithms are concepts that every competitive programmer must know.
Sorting algorithms can be used for collections of numbers, strings, characters, or a
structure of any of these types.
Bubble sort is based on the idea of repeatedly comparing pairs of adjacent elements
and then swapping their positions if they exist in the wrong order.
Assume that A[] is an unsorted array of n elements. This array needs to be sorted in
ascending order. The pseudo code is as follows:
C/C++
Java
Python
def bubbleSort(arr):
n = len(arr)
bubbleSort(arr)
Coding Question-4
LCS Problem Statement: Given two sequences, find the length of longest subsequence
present in both of them. A subsequence is a sequence that appears in the same relative
order, but not necessarily contiguous. For example, “abc”, “abg”, “bdf”, “aeg”,
‘”acefg”, .. etc are subsequences of “abcdefg”. So a string of length n has 2^n
different possible subsequences.
It is a classic computer science problem, the basis of diff (a file comparison program
that outputs the differences between two files), and has applications in bioinformatics.
Examples:
LCS for input Sequences “ABCDGH” and “AEDFHR” is “ADH” of length 3.
LCS for input Sequences “AGGTAB” and “GXTXAYB” is “GTAB” of length 4.
C/C++
int m = strlen(X);
int n = strlen(Y);
Java
char[] X=s1.toCharArray();
char[] Y=s2.toCharArray();
int m = X.length;
int n = Y.length;
Python
if m == 0 or n == 0:
return 0;
elif X[m-1] == Y[n-1]:
return 1 + lcs(X, Y, m-1, n-1);
else:
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
Coding Question-4
SubString Problem in C
Given a string as an input. We need to write a program that will print all non-empty
substrings of that given string.
#include<bits/stdc++.h>
using namespace std;
Java
Coding Question-5
Pythrogorous Triplets
A Pythagorean triplet is a set of three integers a, b and c such that a 2 + b2 = c2. Given a
limit, generate all Pythagorean Triples with values smaller than given limit.
Input : limit = 20
Output : 3 4 5
8 6 10
5 12 13
15 8 17
12 16 20
A Simple Solution is to generate these triplets smaller than given limit using three
nested loop. For every triplet, check if Pythagorean condition is true, if true, then print
the triplet. Time complexity of this solution is O(limit 3) where ‘limit’ is given limit.
An Efficient Solution can print all triplets in O(k) time where k is number of triplets
printed. The idea is to use square sum relation of Pythagorean triplet, i.e., addition of
squares of a and b is equal to square of c, we can write these number in terms of m
and n such that,
a = m 2 - n2
b = 2 * m * n
c = m2 + n2
because,
a2 = m4 + n4 – 2 * m2 * n2
b2 = 4 * m 2 * n 2
c2 = m4 + n4 + 2* m2 * n2
We can see that a2 + b2 = c2, so instead of iterating for a, b and c we can iterate for m
and n and can generate these triplets.
if (c > limit)
break;
// Driver Code
int main()
{
int limit = 20;
pythagoreanTriplets(limit);
return 0;
}
Java
// Java program to generate pythagorean
// triplets smaller than a given limit
import java.io.*;
import java.util.*;
class GFG {
if (c > limit)
break;
System.out.println(a + " " + b + " " + c);
}
m++;
}
}
// Driver Code
public static void main(String args[])
{
int limit = 20;
pythagoreanTriplets(limit);
}
}
Python
# Python3 program to generate pythagorean
# triplets smaller than a given limit
# if c is greater than
# limit then break it
if c > limits :
break
print(a, b, c)
m = m + 1
# Driver Code
if __name__ == '__main__' :
limit = 20
pythagoreanTriplets(limit)
Coding Question-6
Armstrong Number
Given a number x, determine whether the given number is Armstrong number or not. A
positive integer of n digits is called an Armstrong number of order n (order is number
of digits) if.
Example:
Input : 153
Output : Yes
153 is an Armstrong number.
1*1*1 + 5*5*5 + 3*3*3 = 153
Input : 120
Output : No
120 is not a Armstrong number.
1*1*1 + 2*2*2 + 0*0*0 = 9
Input : 1253
Output : No
1253 is not a Armstrong Number
1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723
Input : 1634
Output : Yes
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634
C/C++
// C++ program to determine whether the number is
// Armstrong number or not
#include<bits/stdc++.h>
using namespace std;
Java
// Driver Program
public static void main(String[] args)
{
Armstrong ob = new Armstrong();
int x = 153;
System.out.println(ob.isArmstrong(x));
x = 1253;
System.out.println(ob.isArmstrong(x));
}
}
Python
# If condition satisfies
return (sum1 == x)
# Driver Program
x = 153
print(isArmstrong(x))
x = 1253
print(isArmstrong(x))
Coding Question-7
The square root of a Prime number by checking first if it is a prime number?
Also, you can study other Command Line Programming Questions here on
our TCS Dashboard.
lease also write the code in C/++/Java/Python in the comments section below
Please comment your own version of code in the comment section below –
[code language=”cpp”]
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<math.h>
bool isPrime(int n)
{
if(n<2)
return false;
int i;
for(i=2;i*i<=n;i++)
{
if(n%i==0)
return false;
}
return true;
}
int main(int argc, char *argv[])
{
if(argc==1)
{
printf("No arguments");
return 0;
}
else
{
int n;
n=atoi(argv[1]);
float sq=0;
if(isPrime(n))
{
sq=sqrt(n);
printf("%.2f",sq);
}
else
printf("%.2f",sq);
return 0;
}
}
[/code]
Coding Question-8
Examples:
Input : 16
Output : 20
Input : 10
Output : 12
Input: 33
Output: 41
Algorithm:
For Example:
If the given decimal number is 16.
Step 1: Remainder when 16 is divided by 8 is 0. Therefore, arr[0] = 0.
Step 2: Divide 16 by 8. New number is 16/8 = 2.
Step 3: Remainder when 2 is divided by 8 is 2. Therefore, arr[1] = 2.
Step 4: Divide 2 by 8. New number is 2/8 = 0.
Step 5: Since number becomes = 0. Stop repeating steps and print the array in reverse
order. Therefore the equivalent octal number is 20.
C/C++
// C++ program to convert a decimal
// number to octal number
#include <iostream>
using namespace std;
decToOctal(n);
return 0;
}
Java
// Java program to convert a decimal
// number to octal number
import java.io.*;
class GFG
{
// Function to convert decimal to octal
static void decToOctal(int n)
{
// array to store octal number
int[] octalNum = new int[100];
// driver program
public static void main (String[] args)
{
int n = 33;
decToOctal(n);
}
}
#include
int main(int argc,char *argv[])
{
int n,s=0,b=1,r;
n=atoi(argv[1]);
int c=n;
while(c>0)
{
r=c%8;
s=s+r*b;
c=c/8;
b=b*10;
}
printf("%d",s);
getch();
}
Coding Question-9
return 0;
}
i = 1;
while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
Output
while(binnum > 0)
{
rem = binnum % 10;
decnum = decnum + rem*i;
//System.out.println(rem);
i = i*2;
binnum = binnum/10;
}
i=1;
quot = decnum;
while(quot > 0)
{
octnum[i++] = quot % 8;
quot = quot / 8;
}
}
}
Sample Output:
#include
void main(int argc,char *argv[])
{
long int n,r,c,b=1,s=0;
n=atoi(argv[1]);
c=n;
while(c!=0)
{
r=c%10;
s=s+r*b;
c=c/10;
b=b*2;
}
printf("%lo",s);
getch();
}
Coding Question-10
int main()
{
int year;
if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}
Java
import java.util.Scanner;
public class Check_Leap_Year
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.print("Enter any year:");
int year = s.nextInt();
boolean flag = false;
if(year % 400 == 0)
{
flag = true;
}
else if (year % 100 == 0)
{
flag = false;
}
else if(year % 4 == 0)
{
flag = true;
}
else
{
flag = false;
}
if(flag)
{
System.out.println("Year "+year+" is a Leap Year");
}
else
{
System.out.println("Year "+year+" is not a Leap Year");
}
}
}
Command Line
#include
void main(int argc,char *argv[])
{
int n;
n=atoi(argv[1]);
if(n%4==0)
{
if(n%100==0)
{
if(n%400==0)
printf("Leap Year");
else printf("Not Leap Year");
}
else printf("Leap Year");
}
else
printf("Not Leap Year");
getch(); }
Coding Question-11
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
Java
public class Prime {
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
Command Line
Please comment your own version of code in the comment section below –
#include
int n, i, flag = 0;
n = atol(argv[1]);
if(n%i==0)
flag=1;
break;
if (flag==0)
else
return 0;
Coding Question-12
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
printf("Reversed Number = %d", reversedNumber);
return 0;
}
Java
public class ReverseNumber {
while(num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
Please comment your own version of code in the comment section below –
#include
#include
int main(int argc, char *argv[])
{
if(argc==1)
{
printf("No Arguments");
return 0;
}
else
{
int n,reverseNumber,temp,rem;
n=atoi(argv[1]);
temp=n;
reverseNumber=0;
while(temp)
{
rem=temp%10;
reverseNumber=reverseNumber*10+rem;
temp=temp/10;
}
printf("%d",reverseNumber);
return 0;
}
}
Coding Question-13
int main()
{
char s[1000], r[1000];
int begin, end, count = 0;
printf("Input a string\n");
gets(s);
end = count - 1;
r[begin] = '\0';
printf("%s\n", r);
return 0;
}
Coding Question-14
HFC is also called greatest common divisor (gcd). HCF of two numbers is a largest
positive numbers which can divide both numbers without any remainder. For example
HCF of two numbers 4 and 8 is 2 since 2 is the largest positive number which can
dived 4 as well as 8 without a remainder.
It is clear that any number is not divisible by greater than number itself.
☆In case of more than one numbers, a possible maximum number which can divide
all of the numbers must be minimum of all of that numbers.
Program :
#include<stdio.h>
int gcd(int , int , int);
int main()
{
int i , j , k , g;
scanf("%d %d %d", &i , &j , &k);
g = gcd(i , j , k);
printf("%d",g);
return 0;
}
}
Java
import java.util.*;
Coding Question-15
Second Largest Number in an Array using Command Line
Programming
#include <stdio.h>
#include <limits.h>
int main()
{
int arr[50], i, Size;
int first, second;
Coding Question-16
[code language=”cpp”]
#include <stdio.h>
#include <stdlib.h>
else {
Coding Question-17
int n1,n2;
n1=atoi(argv[1]);
n2=atoi(argv[2]);
n3=n1+n2;
printf(" The sum of the two numbers are %d",n3);
decimal=n3;
while(n3>0)
{
rem=n3%2;
binary=binary+rem*base;
base=base*10;
n3=n3/2;
}
Coding Question-18
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
int status;
char sample[100];
read_string(sample);
status = check_string(sample);
while ( status == 1 )
read_string(sample);
status = check_string(sample);
convert_int(sample);
return 0;
strcpy(sample, “”);
gets(sample);
fflush(stdin);
*/
int i, flag = 0;
flag = 1;
}
else {
flag = 0;
break;
if ( flag == 0 ) {
return 1;
else {
return 0;
*/
*/
int i, sum = 0, j = 0;
j++;
Coding Question-19
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
char c;
fp = fopen("input.txt", "r");
if (fp == NULL) {
exit(1);
c = fgetc(fp);
while ( c != EOF ) {
/* If the character read is a newline or tab or space */
if ( c == '\n')
line_count++;
c = fgetc(fp);
continue;
*/
char_count++;
c = fgetc(fp);
*/
return 0;
Coding Question-20
Here we can take the string and reverse it. Then compare
the original string with the reversed string. If they are the
same then it is a palindrome.
Coding Question-21
*/
#include <stdio.h>
#include <string.h>
int main()
char clear[25];
char token[25];
fflush(stdin);
while ( nitems != 1 ){
}
Solution 2 ( using integer manipulation )
#include <stdio.h>
#include <string.h>
int main() {
scanf(“%d”, &number);
temp = number;
while ( temp ) {
rem = temp%10;
temp = temp/10;
i = 0;
snumber[i++] = token[j];
snumber[i] = ‘\0’;
snumber);
Coding Question-22
*/
#include<stdio.h>
void main() {
scanf(“%u”,&num);
binary(num);
printf(“0”);
else
printf(“1”);
printf(“\n”);
}
Method 2 ( Consecutive Divide by 2 – The traditional
way in which we convert a number to Binary )
Coding Question-23
#include<stdio.h>
#include<stdlib.h>
int main(int c,char *v[])
{
int n1=atoi(v[1]),n2=8,res=0;
while(n2!=0)
{
res=res+n1;
n2--;
}
printf("%d",res);
}
Coding Question-24
n1=atoi(argv[1]);
while(n2!=0)
{
res=res+n1;
n2--;
}
printf("%d",res);
}
Coding Question-25
C/C++
#include <stdio.h>
int main()
{
int x = 10, y = 5;
return 0;
}
Java
class PrepInsta {
Python
x = 10
y=5
# x now becomes 15
x=x+y
# y becomes 10
y=x–y
# x becomes 5
x=x–y
print(“After Swapping: x =”,x ,” y =”, y);
Coding Question-26
Pushing all 0’s
Given an array of random numbers, Push all the zero’s of a given array to the end of
the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be
changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be
same. Expected time complexity is O(n) and extra space is O(1).
Example:
C/C++
// A C++ program to move all zeroes at the end of array
#include <iostream>
using namespace std;
Coding Question-27
Coding Question-28
#include
#define TRUE 1
#define FALSE 0
int main(){
char sentence[1000];
char pattern[25];
int result;
gets(sentence);
printf(“\n\n”);
gets(pattern);
char *p;
*/
p = strstr(sentence, pattern);
if ( p == NULL ){
return FALSE;
else {
printf(“\nThe search string was found in the
sentence\n\n”);
return TRUE;
}
}
Coding Question-29
Examples:
C/C++
// Simple C++ program to find k'th smallest element
#include<iostream>
#include<algorithm>
using namespace std;
Java
// Java code for kth smallest element
// in an array
import java.util.Arrays;
import java.util.Collections;
class GFG
{
// Function to return k'th smallest
// element in a given array
public static int kthSmallest(Integer [] arr,
int k)
{
// Sort the given array
Arrays.sort(arr);
// driver program
public static void main(String[] args)
{
Integer arr[] = new Integer[]{12, 3, 5, 7, 19};
int k = 2;
System.out.print( "K'th smallest element is "+
kthSmallest(arr, k) );
}
}
Coding Question-30
GCD Array of Numbers
The GCD of three or more numbers equals the product of the prime factors common
to all the numbers, but it can also be calculated by repeatedly taking the GCDs of
pairs of numbers.
result = arr[0]
For i = 1 to n-1
result = GCD(result, arr[i])
C/C++
// Driven code
int main()
{
int arr[] = { 2, 4, 6, 8, 16 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << findGCD(arr, n) << endl;
return 0;
}
Java
return result;
}
Coding Question-31
Coding Question-32
#include <stdio.h>
#include <string.h>
int main() {
char sentence[100];
char *p;
gets(sentence);
while ( p != NULL ){
printf(“%s\n”, p);
p = strtok(NULL, “\t”);
return 0;
}
Coding Question-33
*/
#include <stdio.h>
#include <string.h>
int main(){
char input[100];
char output[100];
char temp[100];
char *p;
gets(input);
strcpy(temp, input);
strcpy(output,””);
p = strtok(temp, ” “);
while ( p != NULL ){
strcat(output,p);
strcat(output,” “);
printf(“%s\n”, p);
p = strtok(NULL, ” “);
printf(“%s\n”, input);
printf(“%s\n”, output);
printf(“%d\n”, strlen(output));
return 0;
Coding Question-34
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
int length, i;
spaces = 0, others = 0;
length = strlen(buffer);
alpha++;
if ( islower( buffer[i] ))
lower++;
else
upper++;
else if (isdigit(buffer[i] ))
digit++;
}
else if (isspace(buffer[i])){
spaces++;
}
else if (ispunct(buffer[i])){
punct++;
}
else
others++;
Coding Question-35
Coding Question-36
Write a Program to remove vowels from a string?
Coding Question-37
Write a Program for Matrix Multiplication(Universal Program)
This series is a mixture of 2 series - all the odd terms in this series form a Fibonacci series and all the
even terms are the prime numbers in ascending order.
The value N is a Positive integer that should be read from STDIN. The Nth term that is calculated by
the program should be written to STDOUT. Other than the value of Nth term, no other
characters/strings or message should be written to STDOUT.
For example, when N = 14, the 14th term in the series is 17. So only the value 17 should be printed to
STDOUT.
#include<iostream>
using namespace std;
#define MAX 1000
void fibonacci(int n)
{
int i, t1 = 0, t2 = 1, nextTerm;
for (i = 1; i<=n; i++)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
cout << t1;
}
void prime(int n)
{
int i, j, flag, count =0;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
2. Given a series whose even term creates a separate geometric series and odd term creates another
geometric series.
#include<iostream>
using namespace std;
int main()
{
int n, i, r1, r2;
cout << "nEnter the total number of terms : ";
cin >> n;
cout << "nEnter the common ratio for GP - 1 : ";
cin >> r1;
cout << "nEnter the common ratio for GP - 2 : ";
cin >> r2;
cout << "nThe series isn";
int a = 1, b = 1;
if(n % 2 == 0)
{
for(i = 0; i < n/2; i++)
{
cout << a << " ";
a = a * r1;
cout << b << " ";
b = b * r2;
}
}
else
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
{
for(i = 0; i < n/2; i++)
{
cout << a << " ";
a = a * r1;
cout << b << " ";
b = b * r2;;
}
cout << a << " ";
}
cout << endl;
}
Program to find the area of a circle is discussed here. Area of the circle can be found using the
formula, A = πr2 where r is the radius of the circle. When the radius of the circle is known, the area of
the circle can be calculated using the formula mentioned.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
#include <iostream>
int main()
int year;
if(year%4 == 0)
if( year%100 == 0)
if ( year%400 == 0)
else
else
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
else
return 0;
5. GCD of two numbers | Program to find the GCD or HCF of two numbers
#include<iostream>
int main()
int a,b,gcd;
int i;
gcd = i;
cout << “\nGCD of “<< a << ” and ” << b << ” is ” << gcd;
return 0;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
#include<stdio.h>
int main()
int n,i;
scanf(“%d”,&n);
if(n % i ==0)
break;
else
return 0;
#include <iostream>
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
int main()
int a, b, i, flag;
cin >> a;
cin >> b;
cout << “\nPrime Numbers between ” << a << ” and ” << b <<” : “;
while (a < b)
flag = 0;
if(a % i == 0)
flag = 1;
break;
if (flag == 0)
++a;
return 0;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
#include<iostream>
int main()
int n,i;
int fact,rem;
cin >> n;
int sum = 0;
int temp = n;
while(n)
i = 1,fact = 1;
rem = n % 10;
fact = fact * i;
i++;
n = n / 10;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
if(sum == temp)
else
return 0;
#include<iostream>
int is_Palindrome(int );
int n;
int main()
int palindrome;
cin >> n;
palindrome = is_Palindrome(n);
if(palindrome == 1)
else
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
return 0;
if(aj != 0)
else if(sum == n)
return 1;
else
return 0;
#include<iostream>
int main()
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
temp = number;
while (temp != 0)
temp /= 10;
++n;
temp = number;
while (temp != 0)
remainder = temp ;
temp /= 10;
if(result == number)
else
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
return 0;
// C++ program to print the Armstrong numbers between the two intervals
#include <iostream>
#include <math.h>
int main()
cout << “\nArmstrong numbers between ” << start << ” and ” << end << ” are : “;
temp2 = i;
temp1 = i;
while (temp1 != 0)
temp1 /= 10;
++n;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
while (temp2 != 0)
temp2 /= 10;
if (result == i) {
n = 0;
result = 0;
return 0;
#include<iostream>
int main()
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
int sum = 0, n;
int a = 0;
int b = 1;
cin >> n;
while(sum <= n)
a = b; // swap elements
b = sum;
return 0;
#include
while (n!=0)
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
remainder = n ;
n /= 10;
decimal += remainder*pow(2,i);
++i;
return decimal;
int main()
long int n;
cin >> n;
return 0;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
#include <iostream>
int main()
int a, b, i, flag;
cin >> a;
cin >> b;
cout << “\nPrime Numbers between ” << a << ” and ” << b <<” : “;
while (a < b)
flag = 0;
if(a % i == 0)
flag = 1;
break;
if (flag == 0)
++a;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
return 0;
#include <iostream>
int main()
cin >> n;
while(n != 0)
rem = n ;
n /= 10;
return 0;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
#include <iostream>
int main()
int i, j, count = 0;
count++;
j = count - 1;
rev[i] = str[j];
j--;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
**
***
****
*****
******
#include <iostream>
int main()
int i, j,n;
cin >> n;
return 0;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
#include <iostream>
int main()
int n, c, k, space = 1;
cin >> n;
space = n – 1;
cout << ” “;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
space--;
space = 1;
cout << ” “;
space++;
return 0;
#include <bits/stdc++.h>
int main()
int n,i;
cin >> n;
int arr[n];
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
sort(arr, arr+n);
return 0;
#include<iostream>
if (n==0 || n==1)
return n;
int temp[n];
int j = 0;
int i;
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
if (arr[i] != arr[i+1])
temp[j++] = arr[i];
temp[j++] = arr[n-1];
arr[i] = temp[i];
return j;
int main()
int n;
cin >> n;
int arr[n];
int i;
n = remove_duplicate_elements(arr, n);
Telegram - https://t.me/tcsnqtexam
Telegram - https://t.me/campusdrive
return 0;
Telegram - https://t.me/tcsnqtexam
1.To find GCD of two numbers
#include <stdio.h>
int main(int argc,char *argv[])
{
int a,b,small,i;
a=atoi(argv[1]);
b=atoi(argv[2]);
if(a>b)
small=b;
else
small=a;
for(i=small;i>=1;i--)
{
if((a%i)==0&&(b%i)==0)
{
printf("%d",i);
break;
}
}
return 0;
}
2. To find the lcm of two numbers
#include <stdio.h>
int main(int argc,char *argv[])
{
int a,b,large;
a=atoi(argv[1]);
b=atoi(argv[2]);
if(a>b)
large=a;
else
large=b;
while(1)
{
if((large%a)==0&&(large%b)==0)
{
printf("%d",large);
break;
}
large++;
}
return 0;
}
3. To find the Factorial of a non negative number
#include <stdio.h>
int main(int argc,char *argv[])
{
int n,fact=1,i;
n=atoi(argv[1]);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("%d",fact);
return 0;
}
#include <stdio.h>
#include <stdio.h>
int main(int argc,char *argv[])
{
int n,i,count=0;
n=atoi(argv[1]);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
printf("prime number");
else
printf("not prime number ");
return 0;
}
9.To check whether given number is strong number or
not.
#include<stdio.h>
int fact(int);
int main(int argc, char *argv[])
{
int num,d,n,res=0,i,count=0,x;
n=atoi(argv[1]);
num=n;
x=num;
while(n!=0)
{
n=n/10;
count++;
}
for(i=0;i<count;i++){
if(x>0)
{
d=x%10;
res=res+fact(d);
x=x/10;
}
}
if(res==num)
{
printf("strong number");
}
else printf("not strong number");
return 0;
}
int fact(int x)
{
if(x==0)
return 1;
else
return x*fact(x-1);
}
10. To check whether number is palindrome or not.
#include <stdio.h>
int main(int argc,char *argv[])
{
int num,rev=0,digit,orig;
num=atoi(argv[1]);
orig=num;
while(num>0){
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
if(orig==rev)
{
printf("palindrome");
}
else
printf("not palindrome");
return 0;
}
TCS Ninja programming MCQ’s with answers
Brought to you by the N. Rajeev Reddy | More n More @ www.matterhere.com | Google+ - Facebook/matterhere
This series is a mixture of 2 series – all the odd terms in this series form a Fibonacci series and all the even
terms are the prime numbers in ascending order.
The value N is a Positive integer that should be read from STDIN. The Nth term that is calculated by the
program should be written to STDOUT. Other than the value of Nth term, no other characters/strings or
message should be written to STDOUT.
For example, when N = 14, the 14th term in the series is 17. So only the value 17 should be printed to
STDOUT.
Program:
#include<stdio.h>
#define MAX 1000
void fibonacci(int n)
{
int i, t1 = 0, t2 = 1, nextTerm;
for (i = 1; i<=n; i++)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
printf(―%d‖, t1);
}
void prime(int n)
{
int i, j, flag, count =0;
for (i=2; i<=MAX; i++)
{
flag = 0;
for (j=2; j<i; j++)
{
if(i%j == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
count++;
if(count == n)
{
printf(―%d‖, i);
break;
}
}
}
int main( )
{
int n;
scanf(―%d‖, &n);
if(n%2 == 1)
fibonacci (n/2 + 1);
else
prime(n/2);
return 0;
}
Explanation: Factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less
than or equal to n. For example, The value of 5! is 5*4*3*2*1 = 120
Solution:
#include
int main(int a, char *b[]) //command line arguments
{
int x,y,f=1;
x=atoi(b[1]); //atoi function is to convert a character to integer
for(i=1;i<=x;i++)
{
f=f*i;
}
printf(―%d‖,f);
return 0;
}
TCS Ninja Coding question 2:
Write a c program, to find the area of a circle when the diameter is given, using command line arguments. The
input diameter is an integer and the output area should be a floating point variable with 2 point precision.
Solution:
#include
#define PI 3.14
int main(int a, char *b[]) //command line arguments
{
int d; float area =0;
d= atoi(argv[1]);
area =(float) PI*(d/2)*(d/2);
printf(―%0.2f‖, area); //%0.2f is to print the answer with 2 values after decimal point.
return 0;
}
Solution:
#include
int main(int a, char*b[])
{
int year; year=atoi(b[1]);
if(year%100==0)
{
if(year%400==0)
{
printf(―LEAP YEAR‖);
}
else{
printf(―NOT LEAP YEAR‖); } }
else if(year%4==0)
{
printf(―LEAP YEAR‖);
}
else{
printf(―NOT LEAP YEAR‖);
}
return 0; }
Brought to you by the N. Rajeev Reddy | More n More @ www.matterhere.com | Google+ - Facebook/matterhere
TCS Ninja Coding question 4:
Write a c program, to find the GCD of the given 2 numbers, using command line arguments. The input is 2
integer and the output GCD also should be an integer value.
Solution:
#include
int main(int x, char *y[])
{
inta,b,small,i;
a=atoi(y[1]);
b=atoi(y[2]);
small=a>b?b:a;
for(i=small;i>=1;i–)
{
if((a%i==0)&&(b%i==0))
{
printf(―%d‖,i);
break;
}}
return 0;
}
Solution:
#include
#include
#include int main(int a, char *b[])
{
int number,i,flag = 1;
number = atoi(b[1]);
for(i=2; i<number; i++)
{
if(number%i == 0)
{
flag = 0;
break;
}
}
if(flag == 1)
printf(―%.2f‖,sqrt(number));
else
printf(―0.00‖);
return 0;
}
TCS Ninja Coding question 6:
C Program to check whether a given number is a strong number or not. The given number N, a positive integer,
will be passed to the program using the first command line parameter. If it is a strong number, the output should
be ―YES‖, If it is not a prime number then output should be ―NO‖ to stdout. Other than YES or NO, no other
extra information should be printed to stdout.
Solution:
#include
#include
int main(int a, char *b[])
{
int number, i, temp, sum = 0, factorial = 1;
number = atoi(b[1]);
temp = number;
while(number != 0)
{
int rem = number%10;
for(i=2; i<=rem; i++)
{
factorial = factorial * i;
}
sum = sum + factorial;
number = number/10;
factorial = 1;
}
if(temp == sum)
printf(―YES‖);
else
printf(―NO‖);
return 0;
}
Solution:
#include
#include
int main(int a, char *argv[])
{
int number, count, i;
int b[32];
number = atoi(argv[1]);
count = 0;
while(number != 0)
{
b[count]=number%2;
number = number/2;
count++;
}
for(i=(count-1); i>=0; i–)
printf(―%d‖, b[i]);
return 0;
}
Solution:
#include
int main(int argc, char *argv[])
{
int N1, N2, j, i, count, sum = 0;
N1 =atoi(argv[1]);
N2 =atoi(argv[2]);
for(i=N1+1; i<N2; ++i)
{
count = 0;
for(j=2; j<=(i/2); j++)
{
if(i%j==0)
{
count++;
break;
}
}
if(count==0)
sum = sum + i;
}
printf(―%d‖,sum);
return 0;
}
Brought to you by the N. Rajeev Reddy | More n More @ www.matterhere.com | Google+ - Facebook/matterhere
#include
#include
int main(int a, char *b[])
{
int n, i;
n= atoi(b[1]);
for(i = 0; i <= n; i++)
{
if(n == i * i)
{
printf(―YES‖);
return 0;
}
}
printf(―NO‖);
return 0;
}
Solution:
#include
#include
int main(int a,int *b[])
{
int number, rem, sum = 0;
number = atoi(b[1]);
int copy = number;
while(number != 0)
{
rem =number%10;
sum = sum * 10 + rem;
number = number/10;
}
if(copy == sum)
printf(―Palindrome‖);
else
printf(―Not Palindrome‖);
return 0;
}
#include
int main(int argc, char *argv[])
{
char *str = argv[1];
int i;
for(i =0; str[i] !=‘\0′; i++)
{
if(str[i] == ‗a‘ || str[i] == ‗e‘ || str[i] == ‗i‘ || str[i] == ‗o‘ || str[i] == ‗u‘)
{
str[i] = str[i] – 32;
}
}
printf(―%s‖, str);
return 0;
}
Solution:
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the
number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
Solution:
#include
#include
#include int main(int a, char*b[])
{
int n;
n= atoi(b[1]);
int sum=0;
int temp=n;
int cnt=0;
while(n!=0)
{
n=n/10;
cnt++;
}
n=temp;
while(n!=0)
{
int rem=n%10;
sum=sum+pow(rem,cnt);
n=n/10;
}
if(temp==sum)
{
printf(―yes‖);
}
else
{
printf(―no‖);
}
return 0;
}
Solution:
#include
#include
int main(int a, char *b[])
{
int i, n, t1 = 0, t2 = 1, nextTerm;
n=atoi(b[1]);
for (i = 1; i <= n; ++i)
{
printf(―%d ―, t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
Brought to you by the N. Rajeev Reddy | More n More @ www.matterhere.com | Google+ - Facebook/matterhere
TCS Ninja Programming MCQ’s
1)
#include
int main(int argc, char ** argv)
{
char **items;
int j = 3, i;
items = argv;
for(i = 1; (i%4); i++)
{
int **p = &items[j];
printf(―%c‖, **p);
j–;
}
return 0;
}
The above code is run with three command line parameters mentioned below:
1. PIP
2. Pen
3. Pap
4. Ink
Answer: a
2) Improper formation of which of the following data-structures can cause un-intentional looping of a program
that uses it.
2. Linked list
3. Array
4. Queue
5. Stack
3) What is the data type that occupies the least storage in ―C‖ language?
Answer: char
4) Which of the following is true?
a. Array is a dynamic data structure whose size can be changed while stacks are static data structures whose
sizes are fixed.
b. Array elements can be accessed and modified(elements can be added or removed) only at the ends of the
array while any elements of the stack can be accessed or modified randomly through their indices.
c. An array can have elements of different data types.
d. Elements of a linked-list can be accessed only sequentially.
Answer: d
Answer: b
6) Eesha wrote a function fact( ) in ―C‖ language to calculate factorial of a given number and saved the file as
fact.c. She forgot to code the main function to call this fact function. Will she be able to compile this fact.c
without the main() function?
a. Yes, she can compile provided the compiler option -nostrict-checking is enabled.
b. No, she can not compile as main function is required to compile any C program file.
c. Yes, she can compile as main( ) is not required at compile time.
d. Yes, she can compile and run as the system will supply default values to fact function.
Answer: b
Answer: d
1) The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively. The post-
order traversal of the binary tree is:
a. d e b f g c a
b. d e f g b c a
c. e d b f g c a
d. e d b g f c a
Answer: a
2) Eesha wrote a recursive function that takes the first node in a linked list as an argument, reverses the list,
returning the first Node in the result. The pseudo code for this function is given below. However, she did not
get the correct result. In which line number did she make a mistake?
3) The longest common subsequence (LCS) problem is the problem of finding the longest subsequence
common to a set of sequences (often just two sequences). A subsequence is a sequence that can be derived from
another sequence by deleting some or no elements without changing the order of the remaining elements. One
form of implementation of LCS function is given below. The function takes as input sequences X[1…m] and
Y[1…n], computes the length of the Longest common subsequence between X[1..i] and Y[1..j] for all 1<i < m
and 1< j < n, and stores it in C[i,j]. C[m,n] will contain the length of the LCS of X and Y.
Eesha used the above algorithm to calculate the LCS length between ―kitten‖ and ―string‖. What was the result
she got? Please give the answer in the blank line. ___________
Answer: 2
Brought to you by the N. Rajeev Reddy | More n More @ www.matterhere.com | Google+ - Facebook/matterhere
TCS Ninja Programming MCQ’s (previously asked)
1) How many times the below loop will be executed?
#include
int main()
{
int x, y;
for(x=5;x>=1;x–)
{
for(y=1;y<=x;y++)
printf(―%d\n‖,y);
}}
A. 15
B. 11
C. 10
D. 13
Solution: Option A
A. Disk
B. Stack
C. Heap
D. Code
Solution: Option B
A. double
B. float
C. int
D. long int
int main
{
float f = 0.1;
if (f = 0.1)
printf (―yes‖);
else print (―no‖);
}
5) What will happen if in a C program you assign a value to an array element whose subscript exceeds the size
of array?
A. The element will be set to 0.
B. The compiler would report an error.
C. The program may crash if some important data gets overwritten.
D. The array size would appropriately grow.
Solution: Option C
Explanation: If the index of the array size is exceeded, the program will crash. Hence ―option c‖ is the correct
answer. But the modern compilers will take care of this kind of errors.
int (*ptr)[10];
Solution: Option B
Solution: Option C
Explanation: The statement ‗C‘ is correct. When we pass an array as a function argument, the base address of
the array will be passed.
#include
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf(―%d, %d, %d‖, i, j, m);
return 0;
}
A. 2, 1, 15
B. 1, 2, 5
C. 3, 2, 15
D. 2, 3, 20
Solution: Option C
Explanation:
Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is
initiapzed to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .
Step 2: int i, j, m; The variable i,j,m are declared as an integer type.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
Step 6: printf(―%d, %d, %d‖, i, j, m); It prints the value of the variables i, j, m
Hence the output of the program is 3, 2, 15
A.Yes
B.No
Solution: Option B
Explanation: No, both the statements are same. It is the prototype for the function fun() that accepts one
integer array as a parameter and returns an integer value.
10) Are the expressions arr and &arr same for an array of 10 integers?
A.Yes
B.No
Solution: Option B
Explanation: Both mean two different things. arr gives the address of the first int, whereas the &arr gives the
address of array of ints.
11) Which of the fplowing statements should be used to obtain a remainder after dividing 3.14 by 2.1?
Solution: Option C
Explanation:
fmod(x,y) – Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator. But fmod() performs floating point divisions.
Brought to you by the N. Rajeev Reddy | More n More @ www.matterhere.com | Google+ - Facebook/matterhere
12) What are the types of pnkages?
Solution: Option B
13) Which of the following special symbols are allowed in a variable name?
A.* (asterisk)
B.| (pipepne)
C.-(hyphen)
D._(underscore)
Solution: Option D
Explanation: Variable names in C are made up of letters (upper and lower case) and digits. The underscore
character (―_‖) is also permitted. Names must not begin with a digit.
2 : int fun();
Answer: Option B
Explanation: extern int fun(); declaration in C is to indicate the existence of a global function and it is defined
externally to the current module or in another file.
int fun(); declaration in C is to indicate the existence of a function inside the current module or in the same file.
TCS Ninja English questions
A greenhouse is a glass-covered structure (1) __________ (uses, using, used) to grow plants. It has transparent
glass that allows sunlight to pass (2) ________ (out, through , inside), but does not allow the heat inside to
escape. The same (3) ________ (effect, affect) occurs on the earth. The (4) ________ (sun‘s, suns, sun)
radiation (5) __________ (passes, passing) through the atmosphere to heat the earth‘s surface. When heated, the
earth‘s surface produces infrared radiation, which has a longer wavelength than that of sunlight. This infrared
radiation rises into the atmosphere where gases, such as carbon dioxide, (6) ____________ (prevents, prevent,
prevented) the infrared radiation from escaping into space. The concentrations of these gases, (7) __________
(that, those, which) are called greenhouse gases, control how much-infrared radiation escapes.
(1) used
(2) through
(3) effect
(4) sun‘s
(5) passes
(6) prevent
(7) which
Brought to you by the N. Rajeev Reddy | More n More @ www.matterhere.com | Google+ - Facebook/matterhere
Answer: a
‗I could not put up at a hotel‘
2)
a. A lot of travel delay is caused
b. due to the inefficiency and lack of good management
c. on behalf of the railways.
d. No error.
Answer: c
on the part of the railways
3)
Answer: D
4)
Answer: B
sweeter
5)
Answer: b
Answer: b
7) To have an axe to grind
Answer: a
Answer: d
Answer: a
a. producer:theatre
b. director:drama
c. conductor:bus
d. thespian:play
Answer: d
11) GRAIN:SALT
a. shard:pottery
b. shred:wood
c. blades:grass‘
d. chip:glass
Answer: d
TCS Ninja Aptitude questions
TCS Ninja Mock test questions and solutions – Aptitude (Standard section)
1) A^B means A raised to the power B. If f(x) = ax^4 – bx^2 + x + 5 and f(-3) = 2, then f(3) = ?
a. 3
b. 8
c. -2
d. 1
Answer: b
Explanation:
f(-3) = a(-3)4 – b(-3)2 + (-3) + 5 = 81a – 9b + 2 = 2 So 81a – 9b = 0,
f(3) = a(3)4 – b(3)2 + (3) + 5 = 81a – 9b + 8
Substituting the value of 81a – 9b = 0 in the above we get f(3) = 8
2) 1/4 of the tank contains fuel. When 11 liters of the fuel is poured into the tank, the indicator rests at the 1/2
mark. Find the capacity of the tank in liters.
a. 44
b. 36
c. 6
d. 8
Answer: a
Explanation:
Let the capacity of the tank be x liters.
Given, 1/4 of x + 11 = 1/2 of x
By solving we get the x value as 44 liters.
3) You have been given a physical balance and 7 weights of 47, 46, 43, 48, 49, 42, and 77 kgs. Keeping weights
on one pan and object on the other, what is the maximum you can weigh less than 178 kgs.
a. 172
b. 174
c. 175
d. 177
Answer: b
Explanation:
The maximum weight that can be weighed less than 178 kgs is 174 (48 + 49 + 77 = 174 kgs).
Brought to you by the N. Rajeev Reddy | More n More @ www.matterhere.com | Google+ - Facebook/matterhere
4) How many 6-digit even numbers can be formed from the digits 1, 2, 3, 4, 5, 6 and 7 so that the digits should
not repeat and the second last digit is even?
a. 320
b. 6480
c. 2160
d. 720
Answer: d
Explanation:
To form 6-digit even number, the last digit should be an even number so 3 ways (2, 4, or 6) to fill the last digit
and second last digit also should be even for which it will take 2 ways to fill.
The last two digits are filled in 6 ways( 2 x 3 = 6 ways). The rest of the 4 digits can be filled in 5P4 ways i.e.
120 ways. Hence altogether to fill 6-digit even number = 120 * 6 = 720 ways.
5) Out of a group of swans, 7/2 times the square root of the total number are playing on the shore of the pond.
The remaining 2 are inside the pond. Find the total number of swans.
a. 16
b. 25
c. 4
d. 9
Answer: a
Explanation:
Let the number of swans = x2
x2 = 7x/2 + 2 –> x2 = (7x + 4)/2
2x2 = 7x + 4, —> 2x2 – 7x – 4 = 0
The roots of x are 4, -1/2. Here -1/2 is not possible, so the x value will be 4.
The total number of swans is x2 i.e 16.
6) In a village, every weekend, three-eighth of the men and one-third of the women participate in a social
activity. If the total number of participants is 54, and out of them 18 are men then the total number of men and
women in the village is:
a. 180
b. 156
c. 204
d. 228
Answer: b
Brought to you by the N. Rajeev Reddy | More n More @ www.matterhere.com | Google+ - Facebook/matterhere
Explanation:
3/8th of men and 1/3rd of women participated and given that the total participants are 54.
Out of total participants 54, 18 were men and the rest will be women (54-18 = 36 women). From this, we can
say that –> 3/8 * men = 18, therefore men = 48. And 1/3 of women = 36 –> women = 108.
The total number of men and women in the village is 156.
a. 6/5
b. 4/3
c. 3/25
d. 3/250
Answer: c
Explanation:
Q = 20% of P
M = 30% of Q —> 30% of (20% of P) —> 30/ 100 * 20/100 * P –> 6/100 * p
N = 50% of P –> 5/10 * P
M/ N = (6/100 * P) / (5/10 * P) = 6/50 = 3/25
8) There are 20 persons among whom two are sisters. Find the number of ways in which we can arrange them
around a circle so that there is exactly one person between two sisters? Please note that the exact position on the
circle does not matter (no seat numbers are marked on the circle), and only the relative positions of the people
matter.
a. 2! * 19!
b. None of these
c. 2 * 18!
d. 18!
Answer: c
Explanation:
Fix the position of two sisters. Hence there are only 18 people left
So there are 18 ways in which a person can sit between the two sisters. Now if we swap the bothers we get
another 18 ways.
So hence we have a total of = 2 * 18 combinations
Consider the group of three people(two brothers and the person between them) as a single entity.
we have another 17 people left so there are 18 entities to be arranged in total.
Arranging 18 entities around a circle can be done in (18-1)! = 17! ways
Total no of ways = 2 * 18 * 17! = 2 * 18!
Brought to you by the N. Rajeev Reddy | More n More @ www.matterhere.com | Google+ - Facebook/matterhere
9) Find the length of the longest pole that can be placed in an indoor stadium 24m long, 18m wide and 16m
high.
a. 36m
b. 34m
c. 30m
d. 25m
Answer: b
Explanation:
= √( l²+b²+h²)
= √(24²+18²+16²)
= √(576+324+256)
= √1156
= 34 m
10) Of a set of 30 numbers, the average of first 10 numbers is equal to the average of last 20 numbers. Then the
sum of the last 20 numbers is:
Answer: b
Explanation:
Average = (sum of n numbers)/(n)
(sum of first 10 numbers)/10 = (sum of last 20 numbers)/20
Hence, (sum of last 20 numbers) = 2*(sum of first 10 numbers)
11) Thomas takes 7 days to paint a house completely whereas Raj would require 9 days to paint the same house
completely. How many days will it take to paint the house if both of them work together (give answers to the
nearest integer)?
a. 4 days
b. 2 days
c. 5 days
d. 3 days
Answer: a
Explanation:
Work done by Thomas in a day = 1/7
Work done by Raj in a day = 1/9
Work done by both in a day = 1/7 +1/9 =16/63
Days required if they both work together = 63/16 = 3.9 = 4 days
12) The University of Vikramasila has enrolled nine Ph.D. candidates: Babu, Chitra, Dheeraj, Eesha, Farooq,
Gowri, Hameed, Iqbal, Jacob.
– Farooq and Iqbal were enrolled on the same day as each other, and no one else was enrolled that day.
– Chitra and Gowri were enrolled on the same day as each other, and no one else was enrolled that day.
– On each of the other days of hiring, exactly one candidate was enrolled.
– Eesha was enrolled before Babu.
– Hameed was enrolled before Dheeraj.
– Dheeraj was enrolled after Iqbal but before Eesha.
– Gowri was enrolled after both Jacob and Babu.
– Babu was enrolled before Jacob.
Answer: c
Explanation:
1. Easha < Babu
2. Hameed < Dheeraj
3. Iqbal < Dheeraj < Easha
4. Jacob/Babu < Gowri
5. Babu < Jacob
from 1 and 5, Easha was before Babu and Jacob so she cannot be in the last two. Option B ruled out
from 4 and 5, babu is before Jacob and Gowri so he cannot be in the last two. Options a, c ruled out.
So option d is correct.
13) In a certain city, 60 percent of the registered voters are Party A supporters and the rest are Party B
supporters. In an assembly election, if 75% of the registered Party A supporters and 20% of the registered Party
B supporters are expected to vote for Candidate A, what percent of the registered voters are expected to vote for
Candidate A?
a. 20
b. 60
c. 75
d. 53
Answer: d
Explanation:
let there be x number of registered voters
60% are Party A supporters = 60% of x
40% are Party B supporters = 40% of x
Out of 60%, 75% voted for party A = 75%(60% of x) = 18x/40
Out of 40% ,20% voted for party B = 20%(40% of x) = 8x/100
=18x/40+8x/100=106x/200
Percentage of registered voters expected to vote for A = 106x/200*100 = 53% of x
14) When 100 is to be successively divided by 6, 3, 4, first divide 100 by 6. Then divide the quotient 16 by 3.
Then divide the quotient 5 by 4.
A number when successively divided by 5, 3, 2 gives the remainder of 0, 2 and 1 respectively in that order.
What will be the remainders when the same number is divided successively by 2, 3 and 5 in that order?
a. 4, 1, 2
b. 1, 0, 4
c. 2, 1, 3
d. 4, 3, 2
Answer: b
15) Professor Nitwit obtains a hash number of a given positive integer > 3 as follows. He subtracts 2 from the
number (to get the new number), and multiplies the new number by 2 to get a term. He repeats this with the
new number (to get newer numbers and terms) until the number becomes 2 or 1. The hash is defined as the sum
of all the terms generated in this process.
For example, with the number 5, he multiplies (5-2 =3) by 2 to get the first term 6. He multiplies (3-2=1) by 2
to get the second term 2. As the number has become 1, he stops. The hash is the sum of the two terms (6+2) or
8.
If professor Nitwit is given 3 numbers 4, 9 and 13, what is the sum of the hash numbers he obtains for the three
numbers?
TCS Ninja Mock test questions and solutions – Aptitude (Advanced section)
1) How many pairs (m,n) of integers satisfy the equation 4^m = n^2 + 15? Please do not add white space
around the answer _____________
Answer: 4
2) Of all the nonempty subsets S of { 1, 2, 3, 4, 5, 6, 7}, how many do not contain the number |S|, where |S|
denotes the number of elements in S? For example, {3, 4} is one such subset, since it does not contain the
number 2. Please do not add white space around the answer ____________
Answer: 63
3) A chord of a circle has length 3n, where n is a positive integer. The segment cut off by the chord has height
n, as shown. What is the smallest value of n for which the radius of the circle ia also a positive integer? Please
do not add white space around the answer ________
Answer: 8
4) A function f satisfies f(0) = 0, f(2n) = f(n), and f(2n + 1) = f(n) + 1 for all positive integers n. What is the
value of f(2018)? Please do not add white space around the answer ___________
5) If n is a positive integer, let s(n) denote the integer obtained by removing the last digit of n and placing it in
front. For example, s(731) = 173. What is the smallest positive integer n ending in 6 satisfying s(n) = 4n?.
Please do not add white space around the answer ____________
Brought to you by the N. Rajeev Reddy | More n More @ www.matterhere.com | Google+ - Facebook/matterhere
a. 10
b. 12
c. 11
d. 13
Ans: a
Explanation:
Let x be the number of questions correct and therefore, (26- x) will be the wrong number of questions,
8x – 5(26-x) = 0 –> 8x – 130 + 5x =0
13x = 130, x =10
Hence 10 questions were correct.
2. Jake can dig a well in 16 days. Paul can dig the same well in 24 days. Jake, Paul and Hari together dig the
well in 8 days. Hari alone can dig the well in
a. 96 days
b. 48 days
c. 32 days
d. 24 days
Ans: b
Explanation:
Let the total work to be done is 48 meters( LCM of 16, 24 and 8). Now Jake can dig (48/16) – 3 meters, Paul
can dig (24/12) – 2 meters a day. Now all of them combined dug in 8 days so per day they dug 48/8 = 6 meters.
So Of these 8 meters, Hari capacity is 1 meter. So he takes 48 /1 = 48 days to complete the digging job.
3. Mark told John ―If you give me half your money I will have Rs.75‖. John said, ―if you give me one-third of
your money, I will have Rs.75/- How much money did John have?
a. 45
b. 60
c. 48
d. 37.5
Ans: b
Explanation:
Let the money with Mark and John are M and J respectively.
Now
M + J/2 = 75
M/3 + J = 75
Solving we get M = 45, and J = 60.
4. The value of a scooter depreciates in such a way that its value of the end of each year is 3/4 of its value of the
beginning of the same year. If the initial value of the scooter is Rs.40,000, what is the value at the end of 3
years?
a. Rs.13435
b. Rs.23125
c. Rs.19000
d. Rs.16875
Ans: d
Explanation:
Every year it depreciates 3/4 th of the previous year. So ( 3/ 4 x (3/4 x (3/4 of 40,000))) = 3*3*3*625 = 16875.
Hence the value after 3 years is Rs. 16875
5. A man has a job, which requires him to work 8 straight days and rest on a ninth day. If he started work on
Monday, find the day of the week on which he gets his 12th rest day.
a. Thursday
b. Wednesday
c. Tuesday
d. Friday
Ans: b
Explanation:
He works for 8 days and takes rest on the 9th day. So On the 12th rest day, there are 9 x 12 = 108 days passed.
Number of odd days = (108 – 1) / 7 = 107 / 7 = 2. So the 12th rest day is Wednesday.
6. George can do a piece of work in 10 days, Paul in 12 days and Hari in 15 days. They all start the work
together, but George leaves after 2 days and Paul leaves 3 days before the work is completed. In how many
days is the work completed?
a. 5
b. 6
c. 9
d. 7
Ans: d
Explanation:
Let the work be 60 units(LCM of 10, 12 and 15). If Paul worked for 3 days, and the remaining days of work are
x days, total days to complete the work be x + 3 days. Now George‘s is 60/10 = 6, Paul is 5, Hari is 4.
(6 + 5 + 4) 2 + (5 + 4) (x – 3) + 5 x 3 = 60. On solving we get x = 4. So total days to complete the work is 7
days.
7. How many arrangements will start and end with a vowel for TOGETHER?
a. 1060
b. 1080
c. 2024
d. 1050
Ans: a
Explanation:
No. of ways to put a vowel on start and end = 3 (i.e O..E, E..O, E..E ). The number of ways to arrange other 6
letters = 6!/2! = 360 (letter T is two times). Total number of arrangements = 3*360 = 1080.
8. In 4 years, Raj‘s father age is twice as raj, Two years ago, Raj‘s mother‘s age twice as raj. If Raj is 32 years old in eight
years from now, what is the age of Raj‘s mother and father?
a. 32,34
b. 51,50
c. 32,36
d. 52,46
Ans: d
Explanation:
a. 18 minutes
b. 610 minutes
c. 206 minutes
d. 34 minutes
Ans: a
Explanation:
The call center calls the middle no. i.e. (305/2) = 152.5 say 152 and asks them their name to get an idea of whether to go
to up or downside of 152 no directory and suppose person replies some name. The starting letter of the name will suggest
the call center to decide to weather go up or down the name list.
So the process goes like >305->152–>76–>38–>19–>9–>4–>2–>1, the minimum time = 9*2 = 18 mins.
10. In how many ways a team of 11 must be selected from 5 men and 11 women such that the team must
comprise of not more than 3 men?
a. 1565
b. 2456
c. 1243
d. 2256
Ans: d
Explanation:
The team may consist of 0 men + 11 women, 1 men + 10 women, 2 men + 9 women, or 3 men + 8 women.So
Number of ways are = 11C11+5C1 ×× 11C10+5C2 ×× 11C9+5C3 ××11C8 = 2256 ways.
11. Given that 0 < a < b < c < d, which of the following the largest?
a. (c+d) / (a+b)
b. (b+d) / (a+c)
c. (b+c) / (a+d)
d. (a+d) / (b+c)
Ans: a
Explanation:
Let‘s assume the value of a , b, c and d as 1, 2, 3, 4 (a=1, b=2, c=3, and d=4), by solving we get the answer
as (c+d) / (a+b).
12. Eesha bought 18 sharpeners for Rs.100. She paid 1 rupee more for each white sharpener than for each
brown sharpener. What is the price of a white sharpener and how many white sharpeners did she buy?
a. Rs. 5, 10
b. Rs. 6, 8
c. Rs. 6, 10
d. Rs. 5, 8
Ans: c
Explanation:
Let‘s solve from the options, if she bought 10 white sharpeners at Rs.6 per piece, She has spent Rs.60
already. And with the remaining Rs.40, she bought 8 brown sharpeners at 40/8 = Rs.5 which is Rs.1 less than
the White sharpener. Hence Rs. 6 and 10 white sharpeners.
13. The sum of the digits of a three digit number is 17, and the sum of the squares of its digits is 109. If we
subtract 495 from the number, we shall get a number consisting of the same digits written in the reverse order.
Find the number.
a. 683
b. 863
c. 944
d. 773
Ans: b
Explanation:
Let‘s solve from the options, Sum of the squares should be equal to 109. Only Options a and b satisfying. When
we subtract 495, only 863 becomes 368.
14. Raj goes to the market to buy oranges. If he can bargain and reduce the price per orange by Rs.2, he can buy
30 oranges instead of 20 oranges with the money he has. How much money does he have?
a. Rs. 50
b. Rs. 150
c. Rs. 120
d. Rs. 100
Ans: d
Explanation:
Let the money with Raj is M. So (M/20) – (M/30) = 2. Check options. Option c satisfies.
15. A city in the US has a basketball league with three basketball teams, the Aziecs, the Braves and the
Celtics. A sportswriter notices that the tallest player of the Aziecs is shorter than the shortest player of the
Braves. The shortest of the Celtics is shorter than the shortest of the Aziecs, while the tallest of the Braves is
shorter than the tallest of the Celtics. The tallest of the Braves is taller than the tallest of the Aziecs.
Which of the following can be judged with certainty?
a. Both X and Y
b. X only
c. Y only
d. Neither X nor Y
Ans: B
Explanation:
By assuming the values, let‘s solve it. Be the shortest of Braves is 4 feet, then tallest of Aziecs is less than 4. So
let it be 3 feet. A -> 2 – 3, B -> 4 – 6, C -> 1 – 7. From the above, we can safely conclude X is correct. but Y
cannot be determined.
a. L
b. O
c. K
d. N
Ans: b
Explanation:
Number of letters in each term are in AP. 1, 2, 3, … So, n(n+1)/2 <= 120. For n = 15, we get LHS = 120. So
15th letter in the alphabet is O. So 15th term contains 15 Os.
17. There are 120 male and 100 female in a society. Out of 25% male and 20% female are rural. 20% of male
and 25% of female rural people passed in the exam. What % of rural students have passed the exam?
a. 20%
b. 18%
c. 22%
d. 15%
Ans: c
Explanation:
From the given information, Rural male = 25%(120) = 30, Rural female = 20%(100) = 20. Passed students
from rural: male = 20%(30) = 6, female = 25%(20) = 5. Required percentage = 11/50 * 100 = 22%.
18. On the fabled Island of Knights and Knaves, we meet three people, A, B, and C, one of whom is a knight,
one a knave, and one a spy. The knight always tells the truth, the knave always lies, and the spy can either lie or
tell the truth. A says: ―C is a knave.‖B says: ―A is a knight.‖C says: ―I am the spy.‖Who is the knight, who the
knave, and who the spy?
Ans: d
Explanation:
Let us say A is Knight and speaks the truth. So C is Knave and B is a spy. So Cs statement is false and Bs
statement is true. This case is possible. If B is Knight, this is not possible as A also becomes Knight as B
speaks the truth.
Suppose C is Knight, this is clearly contradicted by C‘s statement itself.
19. The average temperature of Tuesday, Wednesday and Thursday is 37 C. The average temperature of
Wednesday, Thursday and Friday is 38 C. If the temperature on Friday is 39 C. Find the temperature on
Tuesday.
a. 37.33
b. 38.33
c. 36
d. None of the above
Ans: c
Explanation:
The average temperature of Tuesday, Wednesday and Thursday is (Tue + Wed + Thu) / 3 = 37
Tue + Wed + Thu = 111 —— (A)
The average temperature of Wednesday, Thursday and Friday is (Wed + Thu + Fri) / 3 = 38
Wed + Thu + Fri = 114 ——- (B)
Given Friday‘s temperature as 39, then (B) – (A) –> Fri – Tue = 3. So 39 – Tue = 3 –> Tue = 36.
Hence, the temperature on Tuesday is 36
Brought to you by the N. Rajeev Reddy | More n More @ www.matterhere.com | Google+ - Facebook/matterhere
20. In a certain city, 60% of the registered voters are Congress supporters and the rest are BJP supporters. In an
assembly election, if 75% of the registered congress supporters and 20% of the registered BJP supporters are
expected to vote for candidate A, what percent of the registered voters are expected to vote for candidate A?
a. 20
b. 23
c. 50
d. 53
Ans: d
Explanation:
Let the people in the city be 100, Congress supporters = 60% of 100 = 60 and 40% are BJP=40% of 100 = 40.
Out of 60, 75% voted for congress=75%(60)=45
Out of 40%,20% voted for congress=20%(40)=8
In total = 45 + 8 = 53, Hence the total percentage of registered candidates – 53%
Brought to you by the N. Rajeev Reddy | More n More @ www.matterhere.com | Google+ - Facebook/matterhere