Programming Apti
Programming Apti
Set-1
1.
Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1 ?
2.
D. Internal
3.
A. * (asterisk)
B. | (pipeline)
C. - (hyphen)
D. _ (underscore)
4.
D. None of these
5.
A. ceil(1.66)
B. floor(1.66)
C. roundup(1.66)
D. roundto(1.66)
6.
A. float
B. double
C. long double
D. far double
7.
1:
struct book
char name[10];
float price;
int pages;
};
2:
3:
A. 1
B. 2
C. 3
D. Both 1 and 2
8.
extern int i;
A. Declaration
B. Definition
C. Function
D. Error
9.
1: extern int x;
A. 1
B. 2
C. 1 and 3
D. 3
10.
In the following program where is the variable a getting defined and where it is getting declared?
#include<stdio.h>
int main()
extern int a;
printf("%d\n", a);
return 0;
int a=20;
Set-2
1.
#include<stdio.h>
int main()
stud1 = pass;
stud2 = atkt;
stud3 = fail;
A. 0, 1, 2
B. 1, 2, 3
C. 0, 2, 1
D. 1, 3, 2
2.
What will be the output of the program in 16 bit platform (Turbo C under DOS)?
#include<stdio.h>
int main()
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
A. 2
B. 4
3.
#include<stdio.h>
int main()
extern int a;
printf("%d\n", a);
return 0;
int a=20;
A. 20
B. 0
C. Garbage Value
D. Error
4.
What is the output of the program in Turbo C (in DOS 16-bit OS)?
#include<stdio.h>
int main()
char *s1;
return 0;
A. 2, 4, 6
B. 4, 4, 2
C. 2, 4, 4
D. 2, 2, 2
5.
#include<stdio.h>
int main()
{
struct emp
char name[20];
int age;
float sal;
};
return 0;
A. 0, 0.000000
B. Garbage values
C. Error
D. None of above
6.
#include<stdio.h>
int X=40;
int main()
int X=20;
printf("%d\n", X);
return 0;
A. 20
B. 40
C. Error
D. No Output
7.
#include<stdio.h>
int main()
i = x < y < z;
printf("%d\n", i);
return 0;
A. 0
B. 1
C. Error
D. None of these
8.
#include<stdio.h>
int main()
int a;
a = fun(3.14);
printf("%d\n", a);
return 0;
{
return (int)++aa;
A. 3
B. 3.14
C. 0
D. 4
E. Compile Error
9.
#include<stdio.h>
int main()
return 0;
A. Garbage Values
B. 2, 3, 3
C. 3, 2, 2
D. 0, 0, 0
10.
#include<stdio.h>
int main()
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
return 0;
A. 3, 2, 515
B. 515, 2, 3
C. 3, 2, 5
D. None of these
Set-3
1.
#include<stdio.h>
int main()
int x;
if(x < 5)
continue;
else
break;
printf("IndiaBIX");
return 0;
A. Infinite times
B. 11 times
C. 0 times
D. 10 times
2.
How many times the while loop will get executed if a short int is 2 byte wide?
#include<stdio.h>
int main()
int j=1;
j++;
return 0;
A. Infinite times
B. 255 times
C. 256 times
D. 254 times
3.
B. &&
C. ||
D. !
4.
In mathematics and computer programming, which is the correct order of mathematical operators ?
5.
A. Character
B. Integer
C. Float
D. enum
6.
#include<stdio.h>
int main()
if(!(!x) && x)
else
A. y =20
B. x=0
C. x = 10
D. x=1
7.
#include<stdio.h>
int main()
int i=4;
switch(i)
default:
printf("This is default\n");
case 1:
break;
case 2:
break;
case 3:
return 0;
A. This is default
This is case 1
B. This is case 3
This is default
C. This is case 1
This is case 3
D. This is default
8.
#include<stdio.h>
int main()
int i = 1;
switch(i)
printf("Hello\n");
case 1:
printf("Hi\n");
break;
case 2:
printf("\nBye\n");
break;
return 0;
A. Hello
Hi
B. Hello
Bye
C. Hi
D. Bye
9.
#include<stdio.h>
int main()
char j=1;
while(j < 5)
j = j+1;
printf("\n");
return 0;
A. 1 2 3 ... 127
B. 1 2 3 ... 255
D. 1, 2, 3, 4
10.
#include<stdio.h>
int main()
int x, y, z;
x=y=z=1;
z = ++x || ++y && ++z;
return 0;
Set-4
1.
#include<stdio.h>
int main()
int i=0;
printf("%d", i);
return 0;
A. 0, 1, 2, 3, 4, 5
B. 5
C. 1, 2, 3, 4
D. 6
2.
What will be the output of the program?
#include<stdio.h>
int main()
char str[]="C-program";
int a = 5;
return 0;
A. C-program
B. Ps
C. Error
D. None of above
3.
#include<stdio.h>
int main()
b = 300;
c = 200;
return 0;
A. b = 300 c = 200
B. b = 100 c = garbage
C. b = 300 c = garbage
D. b = 100 c = 200
4.
#include<stdio.h>
int main()
while(i++ != 0)
printf("%d",++i);
printf("\n");
return 0;
A. Infinite loop
B. 0 1 2 ... 65535
D. No output
5.
#include<stdio.h>
int main()
int x = 3;
float y = 3.0;
if(x == y)
else
C. Unpredictable
D. No output
6.
What will be the output of the program, if a short int is 2 bytes wide?
#include<stdio.h>
int main()
short int i = 0;
printf("%u,", i);
return 0;
A. 1 ... 65535
C. No output
D. 0, 1, 2, 3, 4, 5
7.
#include<stdio.h>
int main()
char ch;
if(ch = printf(""))
printf("It matters\n");
else
return 0;
A. It matters
B. It doesn't matters
C. matters
D. No output
8.
#include<stdio.h>
int main()
while(i != 0)
printf("%d",++i);
printf("\n");
return 0;
A. Infinite loop
B. 0 1 2 ... 65535
D. No output
9.
#include<stdio.h>
int main()
float a = 0.7;
if(0.7 > a)
printf("Hi\n");
else
printf("Hello\n");
return 0;
A. Hi
B. Hello
C. Hi Hello
D. None of above
10.
#include<stdio.h>
int main()
return 0;
A. 0, 1, 3
B. 1, 2, 3
C. 3, 1, 3
D. 1, 3, 1
Set-5
1.
Which of the following is the correct order of evaluation for the below expression?
z=x+y*z/4%2-1
A. */%+-=
B. =*/%+-
C. /*%-+=
D. *%/-+=
2.
A. /+*-
B. *-/+
C. +-/*
D. /*+-
3.
B. a>b ? c=30;
D. return (a>b)?(a:b)
4.
Which of the following is the correct order if calling functions in the below code?
A. f1, f2, f3
B. f3, f2, f1
D. None of above
5.
1. !
2. sizeof
3. ~
4. &&
A. 1, 2
B. 1, 3
C. 2, 4
D. 1, 2, 3
6.
#include<stdio.h>
int main()
return 0;
A. -2, 3, 1, 1
B. 2, 3, 1, 2
C. 1, 2, 3, 1
D. 3, 3, 1, 2
7.
#include<stdio.h>
int main()
printf("%x\n", -2<<2);
return 0;
A. ffff
B. 0
C. fff8
D. Error
8.
#include<stdio.h>
int main()
return 0;
A. 2, 2, 0, 1
B. 1, 2, 1, 0
C. -2, 2, 0, 0
D. -2, 2, 0, 1
9.
#include<stdio.h>
int main()
z = x!=4 || y == 2;
printf("z=%d\n", z);
return 0;
A. z=0
B. z=1
C. z=4
D. z=2
#include<stdio.h>
int main()
int i = 0;
a[i] = i ;
return 0;
A. 1, 0, 1
B. 1, 1, 1
C. 0, 0, 0
D. 0, 1, 0
Set-6
1.
#include<stdio.h>
int main()
int x=55;
return 0;
A. 1, 40, 1
B. 1, 55, 1
C. 1, 55, 0
D. 1, 1, 1
2.
#include<stdio.h>
int main()
int i=2;
return 0;
A. 3, 4
B. 4, 3
C. 4, 4
3.
#include<stdio.h>
int main()
int k, num=30;
printf("%d\n", num);
return 0;
A. 200
B. 30
C. 100
D. 500
4.
#include<stdio.h>
int main()
char ch;
ch = 'A';
return 0;
A. The letter is a
B. The letter is A
C. Error
D. None of above
5.
#include<stdio.h>
int main()
int i=2;
printf("%d\n", j);
return 0;
A. 4
B. 7
C. 6
D. 5
6.
#include<stdio.h>
int main()
{
w = i || j || k;
x = i && j && k;
y = i || j &&k;
z = i && j || k;
return 0;
A. 1, 1, 1, 1
B. 1, 1, 0, 1
C. 1, 0, 0, 1
D. 1, 0, 1, 1
7.
#include<stdio.h>
int main()
return 0;
A. 1, 2, 0, 1
B. -3, 2, 0, 1
C. -2, 3, 0, 1
D. 2, 3, 1, 1
8.
#include<stdio.h>
int main()
int x=4, y, z;
y = --x;
z = x--;
return 0;
A. 4, 3, 3
B. 4, 3, 2
C. 3, 3, 2
D. 2, 3, 3
9.
#include<stdio.h>
int main()
int i=3;
i = i++;
printf("%d\n", i);
return 0;
A. 3
B. 4
C. 5
D. 6
10.
#include<stdio.h>
int main()
printf("c=%d\n", c);
return 0;
A. c=100
B. c=200
C. c=1
D. c=300
Set-7
1.
#include<stdio.h>
int main()
k = MAN(++i, j++);
printf("%d, %d, %d\n", i, j, k);
return 0;
A. 12, 6, 12
B. 11, 5, 11
C. 11, 5, Garbage
D. 12, 6, Garbage
2.
#include<stdio.h>
int main()
a = 2*(s-u*t)/SQUARE(t);
return 0;
A. Result = -100.000000
B. Result = -25.000000
C. Result = 0.000000
D. Result = 100.000000
3.
#include<stdio.h>
#define SQR(x)(x*x)
int main()
int a, b=3;
a = SQR(b+2);
printf("%d\n", a);
return 0;
A. 25
B. 11
C. Error
D. Garbage value
4.
#include<stdio.h>
#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);
int main()
char *str1="India";
char *str2="BIX";
JOIN(str1, str2);
return 0;
A. str1=IndiaBIX str2=BIX
B. str1=India str2=BIX
C. str1=India str2=IndiaBIX
#include<stdio.h>
int main()
int a, b=3;
a = CUBE(b++);
return 0;
A. 9, 4
B. 27, 4
C. 27, 6
D. Error
6.
#include<stdio.h>
int main()
PRINT(x);
PRINT(y);
PRINT(z);
return 0;
}
7.
#include<stdio.h>
int main()
SWAP(a, b);
return 0;
A. a = 10, b = 12
B. a = 12, b = 10
8.
#include<stdio.h>
int main()
{
int va1=10;
int va12=20;
return 0;
A. 10
B. 20
C. 1020
D. 12
9.
#include<stdio.h>
{\
if(arg)\
printf("IndiaBIX...", "\n");\
}while(--i)
int main()
int i=2;
FUN(i<3);
return 0;
A. IndiaBIX...
IndiaBIX...
IndiaBIX
B. IndiaBIX... IndiaBIX...
C. Error: cannot use control instructions in macro
D. No output
10.
#include<stdio.h>
int main()
int x;
x = MAX(3+2, 2+7);
printf("%d\n", x);
return 0;
A. 8
B. 9
C. 6
D. 5
Set-8
1.
#include<stdio.h>
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
return 0;
A. 2, 1, 15
B. 1, 2, 5
C. 3, 2, 15
D. 2, 3, 20
2.
#include<stdio.h>
int main()
int i, j;
*(*(i+p)+j), *(*(p+j)+i));
}
}
return 0;
A. 1, 1, 1, 1
2, 3, 2, 3
3, 2, 3, 2
4, 4, 4, 4
B. 1, 2, 1, 2
2, 3, 2, 3
3, 4, 3, 4
4, 2, 4, 2
C. 1, 1, 1, 1
2, 2, 2, 2
2, 2, 2, 2
3, 3, 3, 3
D. 1, 2, 3, 4
2, 3, 4, 1
3, 4, 1, 2
4, 1, 2, 3
3.
#include<stdio.h>
int main()
int i;
fun(4, arr);
printf("%d,", arr[i]);
return 0;
int *p=0;
int i=0;
while(i++ < n)
p = &arr[i];
*p=0;
A. 2, 3, 4, 5
B. 1, 2, 3, 4
C. 0, 1, 2, 3
D. 3, 2, 1 0
4.
#include<stdio.h>
int main()
int *ptr;
ptr = &a[0][0];
fun(&ptr);
return 0;
printf("%d\n", **p);
A. 1
B. 2
C. 3
D. 4
5.
#include<stdio.h>
int main()
int **ptr=p;
ptr++;
*ptr++;
*++ptr;
++*ptr;
return 0;
}
A. 0, 0, 0
1, 1, 1
2, 2, 2
3, 3, 3
B. 1, 1, 2
2, 2, 3
3, 3, 4
4, 4, 1
C. 1, 1, 1
2, 2, 2
3, 3, 3
3, 4, 4
D. 0, 1, 2
1, 2, 3
2, 3, 4
3, 4, 5
6.
What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?
#include<stdio.h>
int main()
return 0;
A. 65474, 65476
B. 65480, 65496
C. 65480, 65488
D. 65474, 65488
7.
#include<stdio.h>
int main()
while(i<5)
arr[i]=++i;
return 0;
A. 1, 2, 3, 4, 5,
B. Garbage value, 1, 2, 3, 4,
C. 0, 1, 2, 3, 4,
D. 2, 3, 4, 5, 6,
8.
#include<stdio.h>
int main()
{
int arr[1]={10};
printf("%d\n", 0[arr]);
return 0;
A. 1
B. 10
C. 0
D. 6
9.
What will be the output of the program if the array begins at address 65486?
#include<stdio.h>
int main()
return 0;
A. 65486, 65488
B. 65486, 65486
C. 65486, 65490
D. 65486, 65487
10.
#include<stdio.h>
int main()
{
printf("%d\n", sizeof(arr)/sizeof(arr[0]));
return 0;
A. 5
B. 4
C. 6
D. 7
Set-9
1.
What will be the output of the program in 16 bit platform (Turbo C under DOS)?
#include<stdio.h>
int main()
int fun();
int i;
i = fun();
printf("%d\n", i);
return 0;
int fun()
_AX = 1990;
A. Garbage value
B. 0 (Zero)
C. 1990
D. No output
2.
#include<stdio.h>
int main()
fun(&i, &j);
return 0;
*i = *i**i;
*j = *j**j;
A. 5, 2
B. 10, 4
C. 2, 5
D. 25, 4
3.
#include<stdio.h>
int i;
int fun();
int main()
while(i)
fun();
main();
printf("Hello\n");
return 0;
int fun()
printf("Hi");
A. Hello
B. Hi Hello
C. No output
D. Infinite loop
4.
#include<stdio.h>
int reverse(int);
int main()
int no=5;
reverse(no);
return 0;
if(no == 0)
return 0;
else
printf("%d,", no);
reverse (no--);
A. Print 5, 4, 3, 2, 1
B. Print 1, 2, 3, 4, 5
C. Print 5, 4, 3, 2, 1, 0
D. Infinite loop
5.
#include<stdio.h>
void fun(int);
int main()
int a=3;
fun(a);
return 0;
}
void fun(int n)
if(n > 0)
fun(--n);
printf("%d,", n);
fun(--n);
A. 0, 2, 1, 0,
B. 1, 1, 2, 0,
C. 0, 1, 0, 2,
D. 0, 1, 2, 0,
6.
#include<stdio.h>
int sumdig(int);
int main()
int a, b;
a = sumdig(123);
b = sumdig(123);
return 0;
int sumdig(int n)
int s, d;
if(n!=0)
d = n%10;
n = n/10;
s = d+sumdig(n);
else
return 0;
return s;
A. 4, 4
B. 3, 3
C. 6, 6
D. 12, 12
7.
#include<stdio.h>
int main()
void fun(char*);
char a[100];
fun(&a[0]);
return 0;
a++;
printf("%c", *a);
a++;
printf("%c", *a);
A. AB
B. BC
C. CD
D. No output
8.
#include<stdio.h>
int main()
int fun(int);
int i = fun(10);
printf("%d\n", --i);
return 0;
int fun(int i)
return (i++);
A. 9
B. 10
C. 11
D. 8
9.
#include<stdio.h>
int main()
int c;
c = check(10, 20);
printf("c=%d\n", c);
return 0;
p=&i;
q=&j;
A. Print 10
B. Print 20
C. Print 1
D. Compile error
10.
#include<stdio.h>
int fun(int, int);
int main()
return 0;
return (a==b);
A. 6
B. 1
C. 0
D. -1
Set-10
1.
#include<stdio.h>
int main()
int i=1;
if(!i)
printf("IndiaBIX,");
else
i=0;
printf("C-Program");
main();
return 0;
2.
#include<stdio.h>
kk = ii + jj;
ll = ii * jj;
}
int main()
k = addmult(i, j);
l = addmult(i, j);
return 0;
A. 12 12
B. No error, No output
D. None of above
3.
#include<stdio.h>
int i;
int fun1(int);
int fun2(int);
int main()
extern int j;
int i=3;
fun1(i);
printf("%d,", i);
fun2(i);
printf("%d", i);
return 0;
int fun1(int j)
printf("%d,", ++j);
return 0;
int fun2(int i)
printf("%d,", ++i);
return 0;
int j=1;
A. 3, 4, 4, 3
B. 4, 3, 4, 3
C. 3, 3, 4, 4
D. 3, 4, 3, 4
4.
#include<stdio.h>
int func1(int);
int main()
int k=35;
k = func1(k=func1(k=func1(k)));
printf("k=%d\n", k);
return 0;
}
int func1(int k)
k++;
return k;
A. k=35
B. k=36
C. k=37
D. k=38
5.
#include<stdio.h>
kk = ii + jj;
ll = ii * jj;
int main()
k = addmult(i, j);
l = addmult(i, j);
A. 12, 12
B. 7, 7
C. 7, 12
D. 12, 7
6.
#include<stdio.h>
int check(int);
int main()
int i=45, c;
c = check(i);
printf("%d\n", c);
return 0;
return 100;
else
return 10;
A. 100
B. 10
C. 1
D. 0
7.
#include <stdio.h>
void fun(char**);
int main()
fun(argv);
return 0;
char *t;
t = (p+= sizeof(int))[-1];
printf("%s\n", t);
A. ab
B. cd
C. ef
D. gh
8.
#include<stdio.h>
int fun(int(*)());
int main()
{
fun(main);
printf("Hi\n");
return 0;
printf("Hello ");
return 0;
A. Infinite loop
B. Hi
C. Hello Hi
D. Error
9.
#include<stdio.h>
int fun(int i)
i++;
return i;
int main()
int fun(int);
int i=3;
fun(i=fun(fun(i)));
printf("%d\n", i);
return 0;
A. 5
B. 4
C. Error
D. Garbage value
10.
#include<stdio.h>
int fun(int);
int main()
float k=3;
fun(k=fun(fun(k)));
printf("%f\n", k);
return 0;
int fun(int i)
i++;
return i;
A. 5.000000
B. 3.000000
C. Garbage value
D. 4.000000
Set-11
1.
What is (void*)0?
C. Error
D. None of above
2.
char *p;
p = (char*) malloc(100);
A. char p = *malloc(100);
C. char *p = (char*)malloc(100);
3.
A. stdio.h
B. stddef.h
D. math.h
4.
How many bytes are occupied by near, far and huge pointers (DOS)?
A. near=2 far=4 huge=4
5.
If a variable is a pointer to a structure, then which of the following operator is used to access data
members of the structure through the pointer variable?
A. .
B. &
C. *
D. ->
6.
What would be the equivalent pointer expression for referring the array element a[i][j][k][l]
A. ((((a+i)+j)+k)+l)
B. *(*(*(*(a+i)+j)+k)+l)
C. (((a+i)+j)+k+l)
D. ((a+i)+j+k+l)
7.
A pointer is
8.
B. &
C. &&
D. ||
9. Which of the following functions from “stdio.h” can be used in place of printf()?
10. As per C language standard, which of the followings is/are not keyword(s)? Pick the best
statement. auto make main sizeof elseif
C make main
D auto make
Set-12
1.
#include<stdio.h>
int main()
++p;
printf("%s", **p+1);
return 0;
A. ink
B. ack
C. ite
D. let
2.
#include<stdio.h>
int main()
j = &i;
printf("%d\n", i**j*i+*j);
return 0;
A. 30
B. 27
C. 9
D. 3
3.
#include<stdio.h>
int main()
z=y;
*y++=*z++;
x++;
return 0;
4.
#include<stdio.h>
int main()
*p='M';
printf("%s\n", str);
return 0;
A. Mello
B. Hello
C. HMello
D. MHello
5.
What will be the output of the program If the integer is 4bytes long?
#include<stdio.h>
int main()
p = &i;
q = &p;
r = &q;
return 0;
A. 8, 8, 8
6.
#include<stdio.h>
int i;
int main()
void *vptr;
vptr = &i;
fun(vptr);
return 0;
int **q;
q = (int**)&p;
printf("%d\n", **q);
B. Garbage value
C. 0
D. No output
7.
#include<stdio.h>
int main()
char *str;
str = "%s";
printf(str, "K\n");
return 0;
}
A. Error
B. No output
C. K
D. %s
8.
#include<stdio.h>
int main()
int *c;
c = check(10, 20);
printf("%d\n", c);
return 0;
p = &i;
q = &j;
return (p);
else
return (q);
A. 10
B. 20
C. Error: Non portable pointer conversion
9.
What will be the output of the program if the size of pointer is 4-bytes?
#include<stdio.h>
int main()
return 0;
A. 2, 1
B. 2, 2
C. 4, 1
D. 4, 2
10.
#include<stdio.h>
int main()
void *vp;
int j=65;
vp=&ch;
printf("%c", *(char*)vp);
vp=&j;
printf("%c", *(int*)vp);
vp=cp;
printf("%s", (char*)vp+2);
return 0;
A. JCK
B. J65K
C. JAK
D. JACK
Set-13
1.
Which of the following function sets first n characters of a string to a given character?
A. strinit()
B. strnset()
C. strset()
D. strcset()
2.
A. -1
B. 1
C. 0
D. Yes
3.
B. echo "\\n";
C. printf('\n');
D. printf("\\n");
4.
The library function used to find the last occurrence of a character in a string is
A. strnstr()
B. laststr()
C. strrchr()
D. strstr()
5.
Which of the following function is used to find the first occurrence of a given string in another string?
A. strchr()
B. strrchr()
C. strstr()
D. strnset()
6.
Which of the following function is more appropriate for reading in a multi-word string?
A. printf();
B. scanf();
C. gets();
D. puts();
7.
#include<stdio.h>
int main()
printf("India", "BIX\n");
return 0;
A. Error
B. India BIX
C. India
D. BIX
8.
#include<stdio.h>
int main()
printf("%s\n", str);
return 0;
A. Error
B. IndiaBIX
C. Cannot predict
D. None of above
9.
#include<stdio.h>
int main()
int i;
char *t;
t = names[3];
names[3] = names[4];
names[4] = t;
printf("%s,", names[i]);
return 0;
10.
#include<stdio.h>
#include<string.h>
int main()
printf("%d\n", strlen(str));
return 0;
}
A. 10
B. 6
C. 5
D. 11
Set-14
1.
In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What
will str contain?
A. "I am a boy\r\n\0"
B. "I am a boy\r\0"
C. "I am a boy\n\0"
D. "I am a boy"
2.
What is the purpose of "rb" in fopen() function used below in the code?
FILE *fp;
fp = fopen("source.txt", "rb");
D. None of above
3.
#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("trial", "r");
return 0;
B. A structure which contains a char pointer which points to the first character of a file.
4.
Which of the following operations can be performed on the file "NOTES.TXT" using the below code?
FILE *fp;
fp = fopen("NOTES.TXT", "r+");
A. Reading
B. Writing
C. Appending
5.
To print out a and b given below, which of the following printf() statement will you use?
#include<stdio.h>
float a=3.14;
double b=3.14;
Which files will get closed through the fclose() in the following program?
#include<stdio.h>
int main()
fp = fopen("A.C", "r");
fs = fopen("B.C", "r");
ft = fopen("C.C", "r");
return 0;
B. "B.C" "C.C"
C. "A.C"
D. Error in fclose()
7.
On executing the below program what will be the contents of 'target.txt' file if the source file contains a
line "To err is human"?
#include<stdio.h>
int main()
int i, fss;
ft = fopen(target, "w");
while(1)
ch=getc(fs);
if(ch==EOF)
break;
else
fputc(ch, ft);
return 0;
A. rn
B. Trh
C. err
D. None of above
8.
To scan a and b given below, which of the following scanf() statement will you use?
#include<stdio.h>
float a;
double b;
9.
A. gets()
B. fgets()
10.
#include<stdio.h>
int main()
FILE *fp;
int t;
fp = fopen("DUMMY.C", "w");
t = fileno(fp);
printf("%d\n", t);
return 0;
C. Garbage value
D. Error in fileno()
Set-15
1.
Assunming, integer is 2 byte, What will be the output of the program?
#include<stdio.h>
int main()
printf("%x\n", -1>>1);
return 0;
A. ffff
B. 0fff
C. 0000
D. fff0
2.
If an unsigned int is 2 bytes wide then, What will be the output of the program ?
#include<stdio.h>
int main()
printf("%x\n", ~m);
return 0;
A. ffff
B. 0000
C. ffdf
D. ddfd
3.
Assuming a integer 2-bytes, What will be the output of the program?
#include<stdio.h>
int main()
printf("%x\n", -1<<3);
return 0;
A. ffff
B. fff8
C. 0
D. -1
4.
If an unsigned int is 2 bytes wide then, What will be the output of the program ?
#include<stdio.h>
int main()
~a;
printf("%x\n", a);
return 0;
A. ffff
B. 0000
C. 00ff
D. ddfd
5.
#include<stdio.h>
int main()
printf("%d\n", i<<1);
return 0;
A. 0
B. 256
C. 100
D. 80
6.
#include<stdio.h>
int main()
return 0;
A. 4181
B. 4 >> 1 8 >> 1
D. 24
7.
#include<stdio.h>
int main()
char c=48;
int i, mask=01;
printf("%c", c|mask);
mask = mask<<1;
return 0;
A. 12400
B. 12480
C. 12500
D. 12556
8.
{\
P\
return 0;\
M(P)
A. 1
B. 0
C. -1
D. 2
9.
#include<stdio.h>
int main()
k=i|j;
l=i&j;
m=k^l;
return 0;
A. 0, 0, 0, 0, 0
10.
#include<stdio.h>
int main()
{
printf("%d %d\n", 32<<1, 32<<0);
return 0;
A. Garbage values
B. 64 32
0 32
16 32
0 32
C. All zeros
D. 80
00
32 0
0 16
Set-16
1.
#include<stdio.h>
int main()
int y=128;
printf("%d\n", x);
return 0;
A. 128
B. Garbage value
C. Error
D. 0
2.
#include<stdio.h>
#include<stdlib.h>
union employee
char name[15];
int age;
float salary;
};
int main()
strcpy(e1.name, "K");
return 0;
3.
#include<stdio.h>
int main()
int i=10;
fun(&ptr);
return 0;
int j = 223;
return 0;
A. Address of i
Address of j
B. 10
223
C. Error: cannot convert parameter 1 from 'const int **' to 'int **'
D. Garbage value
4.
#include<stdio.h>
int main()
ptrx = &x;
*ptrx = 10;
printf("%d\n", x);
return 0;
A. 5
B. 10
C. Error
D. Garbage value
5.
#include<stdio.h>
int main()
ptr = &j;
return 0;
D. Garbage value
6.
#include<stdio.h>
int main()
s = str;
while(*s)
printf("%c", *s++);
return 0;
A. Error
B. H
C. Hello
D. Hel
7.
#include<stdio.h>
int get();
int main()
printf("%d", x);
return 0;
int get()
return 20;
A. Garbage value
B. Error
C. 20
D. 0
8.
#include<stdio.h>
*f = 10;
return 0;
}
int main()
fun(&arr[3]);
return 0;
9.
#include<stdio.h>
int main()
printf("%d\n", i++);
return 0;
A. 10
B. 11
C. No output
D. Error: ++needs a value
10.
#include<stdio.h>
int main()
const c = -11;
return 0;
A. Error
B. -11, 34
C. 11, 34
D. None of these
Set-17
1.
Which header file should be included to use functions like malloc() and calloc()?
A. memory.h
B. stdlib.h
C. string.h
D. dos.h
2.
B. malloc(variable_name, 0)
C. free();
D. memalloc(variable_name, 0)
3.
How will you free the memory allocated by the following program?
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4
int main()
int **p, i, j;
return 0;
A. memfree(int p);
B. dealloc(p);
C. malloc(p, 0);
D. free(p);
4.
C All types i.e. int, char and float can be used in “switch” control expression.
int (*p)[5];
A It will result in compile error because there shouldn't be any parenthesis i.e. “int *p[5]” is valid.
B p is a pointer to 5 integers.
7. For a given integer, which of the following operators can be used to “set” and “reset” a
particular bit respectively?
A | and &
B && and ||
C & and |
D || and &&
8.
If integer needs two bytes of storage, then maximum value of an unsigned integer is
A. 216 – 1
B. 215 – 1
C. 216
D. 215
E. None of these
9.
What is the correct value to return to the operating system upon the successful completion of a
program?
A. 1
B. -1
C. 0
E. 2
10.
A. start()
B. system()
C. main()
D. printf()
E. getch()
Set-18
1.
A.
char *ptr[3]();
B.
char *ptr[3];
C.
char (*ptr[3])();
D.
char **ptr[3];
2.
What do the following declaration signify?
int *ptr[30];
3.
A.
char *ptr[3]();
B.
char (*ptr)*[3];
C.
char (*ptr[3])();
D.
char (*ptr)[3];
4.
char *arr[10];
5.
A. pf is a pointer to function.
B. pf is a function pointer.
6.
"A pointer to a function which receives an int pointer and returns float pointer".
A.
float *(ptr)*int;
B.
float *(*ptr)(int)
C.
float *(*ptr)(int*)
D.
float (*ptr)(int)
7.
void *cmp();
8.
void *(ptr)*int;
B.
void *(*ptr)()
C.
void *(*ptr)(*)
D.
void (*ptr)()
9.
int *f();
C. f is a function pointer.
10.
void (*cmp)();
Set-19
1.
What will the function rewind() do?
2.
Input/output function prototypes and macros are defined in which header file?
A. conio.h
B. stdlib.h
C. stdio.h
D. dos.h
3.
Which standard library function will you use to find the last occurance of a character in a string in C?
A. strnchar()
B. strchar()
C. strrchar()
D. strrchr()
4.
What is stderr ?
A. standard error
5.
Does there any function exist to convert the int or float to a string?
A. Yes
B. No
6.
7.
Can you use the fprintf() to display the output on the screen?
A. Yes
B. No
8.
9.
A. float
B. real
C. int
D. double
E. char
10.
What number would be shown on the screen after the following statements of C are executed?
char ch;
int i;
ch = 'G';
i = ch-'A';
printf("%d", i);
A. 5
B. 6
C. 7
D. 8
E. 9
Set-20
1. Suppose that in a C program snippet, followings statements are used.
i) sizeof(int);
ii) sizeof(int*);
iii) sizeof(int**);
Assuming size of pointer is 4 bytes and size of int is also 4 bytes, pick the most correct answer from the
given options.
B. i), ii) and iii) would compile successfully and size of each would be same i.e. 4
C i), ii) and iii) would compile successfully but the size of each would be different and
would be decided at run time.
D ii) and iii) would result in compile error but i) would compile and result in size as 4.
2. Assume int is 4 bytes, char is 1 byte and float is 4 bytes. Also, assume that pointer size is 4 bytes
(i.e. typical case)
char *pChar;
int *pInt;
float *pFloat;
sizeof(pChar);
sizeof(pInt);
sizeof(pFloat);
A. 444
B. 144
C. 148
3.
#include "stdlib.h"
int main()
int *pInt;
int **ppInt1;
int **ppInt2;
pInt = (int*)malloc(sizeof(int));
ppInt1 = (int**)malloc(10*sizeof(int*));
ppInt2 = (int**)malloc(10*sizeof(int*));
free(pInt);
free(ppInt1);
free(*ppInt2);
return 0;
}
A. malloc() for ppInt1 and ppInt2 isn’t correct. It’ll give compile time error.
D. No issue with any of the malloc() and free() i.e. no compile/run time error.
4.
#include "stdio.h"
int main()
void *pVoid;
pVoid = (void*)0;
printf("%lu",sizeof(pVoid));
return 0;
A. Assigning (void *)0 to pVoid isn’t correct because memory hasn’t been allocated. That’s why no
compile error but it'll result in run time error.
B. Assigning (void *)0 to pVoid isn’t correct because a hard coded value (here zero i.e. 0) can’t
assigned to any pointer. That’s why it'll result in compile error.
C. No compile issue and no run time issue. And the size of the void pointer i.e. pVoid would equal
to size of int.
i) int var_9 = 1;
iii) int _ = 3;
Choose the correct statement w.r.t. above variables.
B. Only i) is valid.
6. Let x be an integer which can take a value of 0 or 1. The statement if(x = =0) x = 1; else x = 0; is
equivalent to which one of the following?
A x=1+x;
B x=1—x;
C x=x—1;
D x=1%x;
A abcd
B dcba
C cbad
D cabd
i) printf("%d",8);
ii) printf("%d",090);
iii) printf("%d",00200);
iv) printf("%d",0007000);
B Both i) and ii) would compile. i) will print 8 while ii) will print 90
C All i), ii), iii) and iv) would compile successfully and they will print 8, 90, 200 & 7000 respectively.
D Only i), iii) and iv) would compile successfully. They will print 8, 128 and 3584 respectively.
10. Suppose a C program has floating constant 1.414, what's the best way to convert this as "float"
d data type?
A (float)1.414
B float(1.414)
C 1.414f or 1.414F
A. x=0
B. x=1
C. Compilation fails.
void start()
{
int a = 3;
int b = 4;
System.out.print(" " + 7 + 2 + " ");
System.out.print(a + b);
System.out.print(" " + a + b + " ");
System.out.print(foo() + a + b + " ");
System.out.println(a + b + foo());
}
String foo()
{
return"foo";
}
}
A. 9 7 7 foo 7 7foo
B. 72 34 34 foo34 34foo
C. 9 7 7 foo34 34foo
D. 72 7 34 foo34 7foo
3. What will be the output of the program?
classBoolArray
{
boolean [] b = newboolean[3];
int count = 0;
void test()
{
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
System.out.println("count = " + count);
}
}
A. count = 0
B. count = 2
C. count = 3
D. count = 4
4. Which two statements are equivalent?
3/2
3<2
3*4
3<<2
A. 1 and 2
B. 2 and 3
C. 3 and 4
D. 1 and 4
5.
publicvoid foo( boolean a, boolean b)
{
if( a )
{
System.out.println("A"); /* Line 5 */
}
elseif(a && b) /* Line 7 */
{
System.out.println( "A && B");
}
else/* Line 11 */
{
if ( !b )
{
System.out.println( "notB") ;
}
else
{
System.out.println( "ELSE" ) ;
}
}
}
A. Zero
B. Twelve
C. Default
D. Compilation fails
7. What will be the output of the program?
publicclassTest
{
publicstaticvoid aMethod() throws Exception
{
try/* Line 5 */
{
thrownew Exception(); /* Line 7 */
}
finally/* Line 9 */
{
System.out.print("finally "); /* Line 11 */
}
}
publicstaticvoid main(String args[])
{
try
{
aMethod();
}
catch (Exception e) /* Line 20 */
{
System.out.print("exception ");
}
System.out.print("finished"); /* Line 24 */
}
}
A. finally
B. exception finished
D. Compilation fails
8. Which statement is true for the class java.util.ArrayList?
Set-2
1. What will be the output of the program?
classMyThreadextendsThread
{
publicstaticvoid main(String [] args)
{
MyThread t = new MyThread();
t.start();
System.out.print("one. ");
t.start();
System.out.print("two. ");
}
publicvoid run()
{
System.out.print("Thread ");
}
}
A. Compilation fails
A. DeadLock
B. It print 12 12 12 12
C. Compilation Error
B. Line 6
C. Line 12
D. Line 14
5. What will be the output of the program?
publicclassTest
{
publicstaticint y;
publicstaticvoid foo(int x)
{
System.out.print("foo ");
y = x;
}
publicstaticint bar(int z)
{
System.out.print("bar ");
return y = z;
}
publicstaticvoid main(String [] args )
{
int t = 0;
assert t >0 : bar(7);
assert t >1 : foo(8); /* Line 18 */
System.out.println("done ");
}
}
A. bar
B. bar done
C. foo done
D. Compilation fails
6. Which of the following statements is true?
In an assert statement, the expression after the colon ( : ) can be any Java
A.
expression.
In an assert statement, if the expression after the colon ( : ) does not have a
C.
value, the assert's error message will be empty.
switch (z)
{
case4: System.out.println("4 ");
case5: System.out.println("5 ");
default: assert z <10;
}
if ( z <10 )
assert z >4: z++; /* Line 22 */
System.out.println(z);
}
}
B. Line 12
C. Line 14
D. Line 22
8. What will be the output of the program?
publicclassNFE
{
publicstaticvoid main(String [] args)
{
String s = "42";
try
{
s = s.concat(".5"); /* Line 8 */
double d = Double.parseDouble(s);
s = Double.toString(d);
int x = (int) Math.ceil(Double.valueOf(s).doubleValue());
System.out.println(x);
}
catch (NumberFormatException e)
{
System.out.println("bad number");
}
}
}
A. 42
B. 42.5
C. 43
D. bad number
9. What will be the output of the program?
publicclassTest138
{
publicstaticvoid stringReplace (String text)
{
text = text.replace ('j' , 'c'); /* Line 5 */
}
publicstaticvoid bufferReplace (StringBuffer text)
{
text = text.append ("c"); /* Line 9 */
}
publicstaticvoid main (String args[])
{
String textString = new String ("java");
StringBuffer textBuffer = new StringBuffer ("java"); /* Line 14 */
stringReplace(textString);
bufferReplace(textBuffer);
System.out.println (textString + textBuffer);
}
}
A. java
B. javac
C. javajavac
D. Compile error
10. What will be the output of the program (in jdk1.6 or above)?
publicclassBoolTest
{
publicstaticvoid main(String [] args)
{
Boolean b1 = new Boolean("false");
boolean b2;
b2 = b1.booleanValue();
if (!b2)
{
b2 = true;
System.out.print("x ");
}
if (b1 & b2) /* Line 13 */
{
System.out.print("y ");
}
System.out.println("z");
}
}
A. z
B. xz
C. y z
D. Compilation fails.
Set-3
1. Which of the following class level (nonlocal) variable declarations will not compile?
A. protected int a;
B. transient int b = 3;
D. volatile int d;
2. What will be the output of the program?
publicclassArrayTest
{
publicstaticvoid main(String[ ] args)
{
float f1[ ], f2[ ];
f1 = newfloat[10];
f2 = f1;
System.out.println("f2[0] = " + f2[0]);
}
}
classBextendsA
{ }
D. None of these.
4.
/* Missing statements ? */
publicclassNewTreeSetextendsjava.util.TreeSet
{
publicstaticvoid main(String [] args)
{
java.util.TreeSet t = new java.util.TreeSet();
t.clear();
}
publicvoid clear()
{
TreeMap m = new TreeMap();
m.clear();
}
}
which two statements, added independently at beginning of the program, allow the code to
compile?
1. No statement is required
2. import java.util.*;
3. import.java.util.Tree*;
4. import java.util.TreeSet;
5. import java.util.TreeMap;
A. 1 only
B. 2 and 5
C. 3 and 4
D. 3 and 5
5. What will be the output of the program?
classTwo
{
byte x;
}
classPassO
{
publicstaticvoid main(String [] args)
{
PassO p = new PassO();
p.start();
}
void start()
{
Two t = new Two();
System.out.print(t.x + " ");
Two t2 = fix(t);
System.out.println(t.x + " " + t2.x);
}
A. null null 42
B. 0 0 42
C. 0 42 42
D. 0 0 0
6. What will be the output of the program?
publicclassIf2
{
staticboolean b1, b2;
publicstaticvoid main(String [] args)
{
int x = 0;
if ( !b1 ) /* Line 7 */
{
if ( !b2 ) /* Line 9 */
{
b1 = true;
x++;
if ( 5>6 )
{
x++;
}
if ( !b1 )
x = x + 10;
elseif ( b2 = true ) /* Line 19 */
x = x + 100;
elseif ( b1 | b2 ) /* Line 21 */
x = x + 1000;
}
}
System.out.println(x);
}
}
A. 0
B. 1
C. 101
D. 111
7. What will be the output of the program?
publicclassIf1
{
staticboolean b;
publicstaticvoid main(String [] args)
{
short hand = 42;
if ( hand <50&& !b ) /* Line 7 */
hand++;
if ( hand >50 ); /* Line 9 */
elseif ( hand >40 )
{
hand += 7;
hand++;
}
else
--hand;
System.out.println(hand);
}
}
A. 41
B. 42
C. 50
D. 51
8. What will be the output of the program?
int i = 0;
while(1)
{
if(i == 4)
{
break;
}
++i;
}
System.out.println("i = " + i);
A. i=0
B. i=3
C. i = 4
D. Compilation fails.
9. What will be the output of the program?
int i = 0, j = 5;
tp: for (;;)
{
i++;
for (;;)
{
if(i > --j)
{
break tp;
}
}
System.out.println("i =" + i + ", j = " + j);
A. i = 1, j = 0
B. i = 1, j = 4
C. i = 3, j = 4
D. Compilation fails.
10. What will be the output of the program?
int I = 0;
label:
if (I <2) {
System.out.print("I is " + I);
I++;
continue label;
}
A. I is 0
B. I is 0 I is 1
C. Compilation fails.
A. finished
B. Exception
C. Compilation fails.
D. Arithmetic Exception
2. Which statement is true?
A. A try statement must have at least one corresponding catch block.
B. Multiple catch statements can catch the same class of exception more than once.
classBar
{
Bar()
{
System.out.print("bar");
}
publicvoid go()
{
System.out.print("hi");
}
} /* class Bar ends */
A. Compilation fails.
C. It prints "foobarhi"
D. It prints "barhi"
5.
classBar { }
classTest
{
Bar doBar()
{
Bar b = new Bar(); /* Line 6 */
return b; /* Line 7 */
}
publicstaticvoid main (String args[])
{
Test t = new Test(); /* Line 11 */
Bar newBar = t.doBar(); /* Line 12 */
System.out.println("newBar");
newBar = new Bar(); /* Line 14 */
System.out.println("finishing"); /* Line 15 */
}
}
At what point is the Bar object, created on line 6, eligible for garbage collection?
A. after line 12
B. after line 14
B. After line 8
D. When the instance running this code is made eligible for garbage collection.
7.
publicclassX
{
publicstaticvoid main(String [] args)
{
X x = new X();
X x2 = m1(x); /* Line 6 */
X x4 = new X();
x2 = x4; /* Line 8 */
doComplexStuff();
}
static X m1(X mx)
{
mx = new X();
return mx;
}
}
After line 8 runs. how many objects are eligible for garbage collection?
A. 0
B. 1
C. 2
D. 3
8.
publicclassTest
{
publicvoid foo()
{
assertfalse; /* Line 5 */
assertfalse; /* Line 6 */
}
publicvoid bar()
{
while(true)
{
assertfalse; /* Line 12 */
}
assertfalse; /* Line 14 */
}
}
B. Line 6
C. Line 12
D. Line 14
9. What will be the output of the program?
publicclassSqrtExample
{
publicstaticvoid main(String [] args)
{
double value = -9.0;
System.out.println( Math.sqrt(value));
}
}
A. 3.0
B. -3.0
C. NaN
D. Compilation fails.
10. What will be the output of the program?
String a = "newspaper";
a = a.substring(5,7);
char b = a.charAt(1);
a = a + b;
System.out.println(a);
A. apa
B. app
C. apea
D. apep
Set-5
1.
publicclassTest { }
B. Test(void)
C. public Test( )
D. public Test(void)
2. What is the widest valid returnType for methodA in line 3?
publicclassReturnIt
{
returnType methodA(byte x, double y) /* Line 3 */
{
return (long)x / y * 2;
}
}
A. int
B. byte
C. long
D. double
3. Which two cause a compiler error?
1. float[ ] f = new float(3);
2. float f2[ ] = new float[ ];
3. float[ ]f1 = new float[3];
4. float f3[ ] = new float[3];
5. float f5[ ] = {1.0f, 2.0f, 2.0f};
A. 2, 4
B. 3, 5
C. 4, 5
D. 1, 2
4. What will be the output of the program?
publicclassTest
{
publicstaticvoid main(String args[])
{
classFoo
{
publicint i = 3;
}
Object o = (Object)new Foo();
Foo foo = (Foo)o;
System.out.println("i = " + foo.i);
}
}
A. i=3
B. Compilation fails.
C. i = 5
A. Class A
B. Compilation fails.
A. 53
B. 82
C. 8 3
D. 8 5
7.
import java.awt.Button;
classCompareReference
{
publicstaticvoid main(String [] args)
{
float f = 42.0f;
float [] f1 = newfloat[2];
float [] f2 = newfloat[2];
float [] f3 = f1;
long x = 42;
f1[0] = 42.0f;
}
}
B. 2, 4 and 5
C. 3, 4 and 5
D. 1, 4 and 5
8. What will be the output of the program?
publicclassMyProgram
{
publicstaticvoid main(String args[])
{
try
{
System.out.print("Hello world ");
}
finally
{
System.out.println("Finally executing ");
}
}
}
A. Nothing. The program will not compile because no exceptions are specified.
B. Nothing. The program will not compile because no catch clauses are specified.
C. Hello world.
D. Code output: Start Hello world Catch Here File not found.
10. Which class does not override the equals() and hashCode() methods, inheriting them directly
from class Object?
A. java.lang.String
B. java.lang.Double
C. java.lang.StringBuffer
D. java.lang.Character
Set-6
1. You need to store elements in a collection that guarantees that no duplicates are stored and all
elements can be accessed in natural order. Which interface provides that capability?
A. java.util.Map
B. java.util.Set
C. java.util.List
D. java.util.Collection
2.
/* Missing Statement ? */
publicclassfoo
{
publicstaticvoid main(String[]args)throws Exception
{
java.io.PrintWriter out = new java.io.PrintWriter();
new java.io.OutputStreamWriter(System.out,true);
out.println("Hello");
}
}
What line of code should replace the missing statement to make this program compile?
A. No statement required.
B. import java.io.*;
C. include java.io.*;
D. import java.io.PrintWriter;
3. Which is true about an anonymous inner class?
A. It can extend exactly one class and implement exactly one interface.
B. It can extend exactly one class and can implement multiple interfaces.
which statement, if placed in a class other than MyOuter or MyInner, instantiates an instance of
the nested class?
A. MyOuter.MyInner m = new MyOuter.MyInner();
B. MyOuter.MyInner mi = new MyInner();
B. 2 and 4
C. 1 and 2
D. 2 and 5
6. Which two of the following methods are defined in class Thread?
1. start()
2. wait()
3. notify()
4. run()
5. terminate()
A. 1 and 4
B. 2 and 3
C. 3 and 4
D. 2 and 4
7. Under which conditions will a currently executing thread stop?
1. When an interrupted exception occurs.
2. When a thread of higher priority is ready (becomes runnable).
3. When the thread creates a new thread.
4. When the stop() method is called.
A. 1 and 3
B. 2 and 4
C. 1 and 4
D. 2 and 3
8. Which statement is true?
A. The notifyAll() method must be called from a synchronized context.
When is the Float object, created in line 3, eligible for garbage collection?
String a = "ABCD";
String b = a.toLowerCase();
b.replace('a','d');
b.replace('b','c');
System.out.println(b);
A. abcd
B. ABCD
C. dccd
D. dcba
Set-7
1. You want subclasses in any package to have access to members of a superclass. Which is the
most restrictive access that accomplishes this objective?
A. public
B. private
C. protected
D. transient
2.
interfaceBase
{
boolean m1 ();
byte m2(short s);
}
A. 1 and 2
B. 2 and 3
C. 3 and 4
D. 1 and 5
3.
classA
{
protectedint method1(int a, int b)
{
return0;
}
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
A. true true
B. false true
C. true false
D. false false
5. What will be the output of the program?
classPassS
{
publicstaticvoid main(String [] args)
{
PassS p = new PassS();
p.start();
}
void start()
{
String s1 = "slip";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
}
A. slip stream
B. slipstream stream
void start()
{
int x = 7;
twice(x);
System.out.print(x + " ");
}
void twice(int x)
{
x = x*2;
s = x;
}
}
A. 77
B. 7 14
C. 14 0
D. 14 14
7.
import java.awt.*;
classTickerextendsComponent
{
publicstaticvoid main (String [] args)
{
Ticker t = new Ticker();
/* Missing Statements ? */
}
}
which two of the following statements, inserted independently, could legally be inserted into
missing section of this code?
1. boolean test = (Component instanceof t);
2. boolean test = (t instanceof Ticker);
3. boolean test = t.instanceof(Ticker);
4. boolean test = (t instanceof Component);
A. 1 and 4
B. 2 and 3
C. 1 and 3
D. 2 and 4
8.
publicvoid test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}
D. "odd" will be output for odd values of x, and "even" for even values.
9. What will be the output of the program?
int i = 1, j = 10;
do
{
if(i > j)
{
break;
}
j--;
} while (++i <5);
System.out.println("i = " + i + " and j = " + j);
A. i = 6 and j = 5
B. i = 5 and j = 5
C. i = 6 and j = 4
D. i = 5 and j = 6
10. What will be the output of the program?
publicclassTest
{
publicstaticvoid main(String args[])
{
int i = 1, j = 0;
switch(i)
{
case2: j += 6;
case4: j += 1;
default: j += 2;
case0: j += 4;
}
System.out.println("j = " + j);
}
}
A. j=0
B. j=2
C. j = 4
D. j = 6
Set-8
1.
import java.io.*;
publicclassMyProgram
{
publicstaticvoid main(String args[])
{
FileOutputStream out = null;
try
{
out = new FileOutputStream("test.txt");
out.write(122);
}
catch(IOException io)
{
System.out.println("IO Error.");
}
finally
{
out.close();
}
}
}
and given that all methods of class FileOutputStream, including close(), throw
an IOException, which of these is true?
C. Any statement that can throw an Error must be enclosed in a try block.
D. Any statement that can throw an Exception must be enclosed in a try block.
3. Which is valid declaration of a float?
A. float f = 1F;
B. float f = 1.0;
C. float f = "1";
D. float f = 1.0d;
4. What will be the output of the program?
import java.util.*;
classH
{
publicstaticvoid main (String[] args)
{
Object x = new Vector().elements();
System.out.print((x instanceof Enumeration)+",");
System.out.print((x instanceof Iterator)+",");
System.out.print(x instanceof ListIterator);
}
}
A. Prints: false,false,false
B. Prints: false,false,true
C. Prints: false,true,false
D. Prints: true,false,false
5. What will be the output of the program?
TreeSet map = new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it = map.iterator();
while (it.hasNext() )
{
System.out.print( it.next() + " " );
}
B. 2, 5 and 6
C. 3, 4 and 7
D. 4, 5 and 7
8. Which will contain the body of the thread?
A. run();
B. start();
C. stop();
D. main();
9. What will be the output of the program?
publicclassTest
{
publicstaticvoid main(String[] args)
{
int x = 0;
assert (x >0) ? "assertion failed" : "assertion passed" ;
System.out.println("finished");
}
}
A. finished
B. Compiliation fails.
A. 0
B. 1
C. 10
D. 10010
Set-9
1. Given a method in a protected class, what access modifier do you use to restrict access to that
method to only the other members of the same class?
A. final
B. static
C. private
D. protected
E. volatile
2.
interfaceDoMath
{
double getArea(int rad);
}
interfaceMathPlus
{
double getVol(int b, int h);
}
/* Missing Statements ? */
which two code fragments inserted at end of the program, will allow to compile?
1. class AllMath extends DoMath { double getArea(int r); }
2. interface AllMath implements MathPlus { double getVol(int x, int y); }
3. interface AllMath extends DoMath { float getAvg(int h, int l); }
4. class AllMath implements MathPlus { double getArea(int rad); }
5. abstract class AllMath implements DoMath, MathPlus { public double
getArea(int rad) { return rad * rad * 3.14; } }
A. 1 only
B. 2 only
C. 3 and 5
D. 1 and 4
3. Which three statements are true?
1. The default constructor initialises method variables.
2. The default constructor has the same access as its class.
3. The default constructor invokes the no-arg constructor of the superclass.
4. If a class lacks a no-arg constructor, the compiler always creates a default constructor.
5. The compiler creates a default constructor only when there are no other constructors for the
class.
A. 1, 2 and 4
B. 2, 3 and 5
C. 3, 4 and 5
D. 1, 2 and 3
4. What will be the output of the program?
classTest
{
publicstaticvoid main(String [] args)
{
int x=20;
String sup = (x <15) ? "small" : (x <22)? "tiny" : "huge";
System.out.println(sup);
}
}
A. small
B. tiny
C. huge
D. Compilation fails
5. Which of the following are legal lines of code?
1. int w = (int)888.8;
2. byte x = (byte)1000L;
3. long y = (byte)100;
4. byte z = (byte)100L;
A. 1 and 2
B. 2 and 3
C. 3 and 4
B. 2 and 4
C. 3 and 4
D. 1 and 3
7. What will be the output of the program?
publicclassTest
{
publicstaticvoid main(String [] args)
{
int I = 1;
do while ( I <1 )
System.out.print("I is " + I);
while ( I >1 ) ;
}
}
A. I is 1
B. I is 1 I is 1
C. No output is produced.
D. Compilation error
8. What will be the output of the program?
publicclassRTExcept
{
publicstaticvoid throwit ()
{
System.out.print("throwit ");
thrownew RuntimeException();
}
publicstaticvoid main(String [] args)
{
try
{
System.out.print("hello ");
throwit();
}
catch (Exception re )
{
System.out.print("caught ");
}
finally
{
System.out.print("finally ");
}
System.out.println("after ");
}
}
B. Compilation fails
A. AC
B. BC
C. ACD
D. ABCD
10. Which of the following are Java reserved words?
1. run
2. import
3. default
4. implement
A. 1 and 2
B. 2 and 3
C. 3 and 4
D. 2 and 4
Set-10
1. What will be the output of the program?
publicclassTest
{
publicstaticvoid main (String[] args)
{
String foo = args[1];
String bar = args[2];
String baz = args[3];
System.out.println("baz = " + baz); /* Line 8 */
}
}
A. baz =
B. baz = null
C. baz = blue
D. Runtime Exception
2. What will be the output of the program?
classHappyextendsThread
{
final StringBuffer sb1 = new StringBuffer();
final StringBuffer sb2 = new StringBuffer();
new Thread()
{
publicvoid run()
{
synchronized(this)
{
h.sb1.append("A");
h.sb2.append("B");
System.out.println(h.sb1);
System.out.println(h.sb2);
}
}
}.start();
new Thread()
{
publicvoid run()
{
synchronized(this)
{
h.sb1.append("D");
h.sb2.append("C");
System.out.println(h.sb2);
System.out.println(h.sb1);
}
}
}.start();
}
}
A. ABBCAD
B. ABCBCAD
C. CDADACB
A. Compilation fails.
B. 1..2..3..
C. 0..1..2..3..
D. 0..1..2..
4. Which statement is true?
A. A static method cannot be synchronized.
If a class has synchronized code, multiple threads can still access the
B.
nonsynchronized code.
Variables can be protected from concurrent access problems by marking them with
C.
the synchronizedkeyword.
if (o == oc)
result = 1;
if (o != oc)
result = result + 10;
if (o.equals(oc) )
result = result + 100;
if (oc.equals(o) )
result = result + 1000;
A. 1
B. 10
C. 101
D. 1101
8. What will be the output of the program?
publicclassTest178
{
publicstaticvoid main(String[] args)
{
String s = "foo";
Object o = (Object)s;
if (s.equals(o))
{
System.out.print("AAA");
}
else
{
System.out.print("BBB");
}
if (o.equals(s))
{
System.out.print("CCC");
}
else
{
System.out.print("DDD");
}
}
}
A. AAACCC
B. AAADDD
C. BBBCCC
D. BBBDDD
9. What will be the output of the program?
int i = 1, j = 10;
do
{
if(i++ > --j) /* Line 4 */
{
continue;
}
} while (i <5);
System.out.println("i = " + i + "and j = " + j); /* Line 9 */
A. i = 6 and j = 5
B. i = 5 and j = 5
C. i = 6 and j = 6
D. i = 5 and j = 6
10. What will be the output of the program?
publicclassExamQuestion7
{
staticint j;
staticvoid methodA(int i)
{
boolean b;
do
{
b = i<10 | methodB(4); /* Line 9 */
b = i<10 || methodB(8); /* Line 10 */
}while (!b);
}
staticboolean methodB(int i)
{
j += i;
returntrue;
}
publicstaticvoid main(String[] args)
{
methodA(0);
System.out.println( "j = " + j );
}
}
A. j=0
B. j=4
C. j = 8
B. abstract
C. protected
D. synchronized
E. default access
2. You want a class to have access to members of another class in the same package. Which is
the most restrictive access that accomplishes this objective?
A. public
B. private
C. protected
D. default access
3. Which is a valid declaration within an interface?
A. public static short stop = 23;
B. 2 and 4
C. 3 and 5
D. 4 and 6
5. What will be the output of the program?
publicclassSwitch2
{
finalstaticshort x = 2;
publicstaticint y = 0;
publicstaticvoid main(String [] args)
{
for (int z=0; z <3; z++)
{
switch (z)
{
case x: System.out.print("0 ");
case x-1: System.out.print("1 ");
case x-2: System.out.print("2 ");
}
}
}
}
A. 012
B. 012122
C. 2 1 0 1 0 0
D. 2 1 2 0 1 2
6. What will be the output of the program?
publicclassSwitch2
{
finalstaticshort x = 2;
publicstaticint y = 0;
publicstaticvoid main(String [] args)
{
for (int z=0; z <3; z++)
{
switch (z)
{
case y: System.out.print("0 "); /* Line 11 */
case x-1: System.out.print("1 "); /* Line 12 */
case x: System.out.print("2 "); /* Line 13 */
}
}
}
}
A. 012
B. 012122
A. 0 def 1
B. 2 1 0 def 1
C. 2 1 0 def def
D. 2 1 0 def 1 def 1
8. Which collection class allows you to grow or shrink its size and provides indexed access to its
elements, but whose methods are not synchronized?
A. java.util.HashSet
B. java.util.LinkedHashSet
C. java.util.List
D. java.util.ArrayList
9. What will be the output of the program?
publicclassTest
{
publicstaticvoid main (String args[])
{
String str = NULL;
System.out.println(str);
}
}
A. NULL
B. Compile Error
D. Runtime Exception
10. Which of the following will not directly cause a thread to stop?
A. notify()
B. wait()
C. InputStream access
D. sleep()
Set-12
1. What will be the output of the program?
publicclassSyncTest
{
publicstaticvoid main (String [] args)
{
Thread t = new Thread()
{
Foo f = new Foo();
publicvoid run()
{
f.increase(20);
}
};
t.start();
}
}
classFoo
{
privateint data = 23;
publicvoid increase(int amt)
{
int x = data;
data = x + amt;
}
}
and assuming that data must be protected from corruption, what—if anything—can you add to
the preceding code to ensure the integrity of data?
A. Synchronize the run method.
new Thread()
{
publicvoid run()
{
synchronized(sb1)
{
sb1.append("C");
sb2.append("D");
}
}
}.start(); /* Line 28 */
D. Cannot be determined.
3. Which statement is true?
If only one thread is blocked in the wait method of an object, and another thread
A. executes the modify on that same object, then the first thread immediately resumes
execution.
If a thread is blocked in the wait method of an object, and another thread executes
B. the notify method on the same object, it is still possible that the first thread might
never resume execution.
If a thread is blocked in the wait method of an object, and another thread executes
C. the notify method on the same object, then the first thread definitely resumes
execution as a direct and sole consequence of the notify call.
If two threads are blocked in the wait method of one object, and another thread
executes the notify method on the same object, then the first thread that executed
D.
the wait call first definitely resumes execution as a direct and sole consequence of
the notify call.
4. Which statement is true?
A. Memory is reclaimed by calling Runtime.gc().
B. Objects are not collected if they are accessible from live threads.
Objects that have finalize() methods always have their finalize() methods
D.
called before the program ends.
5. What will be the output of the program (when you run with the -ea option) ?
publicclassTest
{
publicstaticvoid main(String[] args)
{
int x = 0;
assert (x >0) : "assertion failed"; /* Line 6 */
System.out.println("finished");
}
}
A. finished
B. Compilation fails.
C. An AssertionError is thrown.
switch (z)
{
case4: System.out.println("4 ");
case5: System.out.println("5 ");
default: assert z <10;
}
if ( z <10 )
assert z >4: z++; /* Line 22 */
System.out.println(z);
}
}
B. Line 12
C. Line 14
D. Line 22
7. Which statement is true?
A. Assertions can be enabled or disabled on a class-by-class basis.
A. abcXyZ
B. abcxyz
C. xyzabc
D. XyZabc
9. What will be the output of the program?
classTree { }
classPineextendsTree { }
classOakextendsTree { }
publicclassForest1
{
publicstaticvoid main (String [] args)
{
Tree tree = new Pine();
if( tree instanceof Pine )
System.out.println ("Pine");
elseif( tree instanceof Tree )
System.out.println ("Tree");
elseif( tree instanceof Oak )
System.out.println ( "Oak" );
else
System.out.println ("Oops ");
}
}
A. Pine
B. Tree
C. Forest
D. Oops
10. What two statements are true about the result obtained from calling Math.random()?
1. The result is less than 0.0.
2. The result is greater than or equal to 0.0..
3. The result is less than 1.0.
4. The result is greater than 1.0.
5. The result is greater than or equal to 1.0.
A. 1 and 2
B. 2 and 3
C. 3 and 4
D. 4 and 5
Set-13
1. Which cause a compiler error?
A. int[ ] scores = {3, 5, 7};
B. 2, 3 and 5
C. 3, 4, and 5
D. 2 and 4
3. What will be the output of the program?
classSuper
{
public Integer getLength()
{
returnnew Integer(4);
}
}
publicclassSubextendsSuper
{
public Long getLength()
{
returnnew Long(5);
}
A. 4, 4
B. 4, 5
C. 5, 4
D. Compilation fails.
4. What will be the output of the program?
classTest
{
publicstaticvoid main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z <5; z++)
{
if (( ++x >2 ) && (++y >2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
A. 52
B. 53
C. 6 3
D. 6 4
5. What will be the output of the program?
int i = l, j = -1;
switch (i)
{
case0, 1: j = 1; /* Line 4 */
case2: j = 2;
default: j = 0;
}
System.out.println("j = " + j);
A. j = -1
B. j=0
C. j = 1
D. Compilation fails.
6. What will be the output of the program?
for(int i = 0; i <3; i++)
{
switch(i)
{
case0: break;
case1: System.out.print("one ");
case2: System.out.print("two ");
case3: System.out.print("three ");
}
}
System.out.println("done");
A. done
A. a
B. b
C. c
D. d
8. What will be the output of the program?
classExc0extendsException { }
classExc1extendsExc0 { } /* Line 2 */
publicclassTest
{
publicstaticvoid main(String args[])
{
try
{
thrownew Exc1(); /* Line 9 */
}
catch (Exc0 e0) /* Line 11 */
{
System.out.println("Ex0 caught");
}
catch (Exception e)
{
System.out.println("exception caught");
}
}
}
A. Ex0 caught
B. exception caught
The program will print Hello world, then will print that a RuntimeException has
B.
occurred, then will print Done with try block, and then will print Finally executing.
The program will print Hello world, then will print that a RuntimeException has
C.
occurred, and then will print Finally executing.
The program will print Hello world, then will print Finally executing, then will print
D.
that a RuntimeException has occurred.
10. Which interface provides the capability to store objects using a key-value pair?
A. Java.util.Map
B. Java.util.Set
C. Java.util.List
D. Java.util.Collection
Set-14
1. Which of the following statements about the hashcode() method are incorrect?
1. The value returned by hashcode() is used in some collection classes to help locate objects.
2. The hashcode() method is required to return a positive int value.
3. The hashcode() method in the String class is the one inherited from Object.
4. Two new empty String objects will produce identical hashcodes.
A. 1 and 2
B. 2 and 3
C. 3 and 4
D. 1 and 4
2. What will be the output of the program?
publicclassTestObj
{
publicstaticvoid main (String [] args)
{
Object o = new Object() /* Line 5 */
{
publicboolean equals(Object obj)
{
returntrue;
}
} /* Line 11 */
System.out.println(o.equals("Fred"));
}
}
A. It prints "true".
B. It prints "Fred".
D. Compilation fails
3. Which of the following will directly stop the execution of a Thread?
A. wait()
B. notify()
C. notifyall()
A. Object
B. Thread
C. Runnable
D. Class
5. What will be the output of the program?
publicclassThreadDemo
{
privateint count = 1;
publicsynchronizedvoid doSomething()
{
for (int i = 0; i <10; i++)
System.out.println(count++);
}
publicstaticvoid main(String[] args)
{
ThreadDemo demo = new ThreadDemo();
Thread a1 = new A(demo);
Thread a2 = new A(demo);
a1.start();
a2.start();
}
}
classAextendsThread
{
ThreadDemo demo;
public A(ThreadDemo td)
{
demo = td;
}
publicvoid run()
{
demo.doSomething();
}
}
C. It will print the numbers 1 to 20, but the order cannot be determined
A. 1 and 2
B. 2 and 3
C. 1 and 4
D. 3 and 4
7. The following block of code creates a Thread using a Runnable target:
Runnable target = new MyRunnable();
Thread myThread = new Thread(target);
Which of the following classes can be used to create the target, so that the preceding code
compiles correctly?
A. public class MyRunnable extends Runnable{public void run(){}}
Select how you would start the program to cause it to print: Arg is 2
B. java Myfile 1 2 2 3 4
C. java Myfile 1 3 2 2
D. java Myfile 0 1 2 3
9. What will be the output of the program?
int i = (int) Math.random();
A. i=0
B. i=1
C. value of i is undetermined
C. Compile Error
D. Prints "complete"
Set-15
1. Which two of the following are legal declarations for nonnested classes and interfaces?
1. final abstract class Test {}
2. public static interface Test {}
3. final public class Test {}
4. protected abstract class Test {}
5. protected interface Test {}
6. abstract public class Test {}
A. 1 and 4
B. 2 and 5
C. 3 and 6
D. 4 and 6
2.
publicclassWhile
{
publicvoid loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
}
}
}
A. 1
B. 2
C. 3
D. 4
4. What will be the output of the program?
publicclassTest
{
privatestaticfloat[] f = newfloat[2];
publicstaticvoid main (String[] args)
{
System.out.println("f[0] = " + f[0]);
}
}
A. f[0] = 0
B. f[0] = 0.0
C. Compile Error
D. Runtime Exception
5. Which two statements are true about comparing two instances of the same class, given that
the equals() and hashCode() methods have been properly overridden?
1. If the equals() method returns true, the hashCode() comparison == must return true.
2. If the equals() method returns false, the hashCode() comparison != must return true.
3. If the hashCode() comparison == returns true, the equals() method must return true.
4. If the hashCode() comparison == returns true, the equals() method might return true.
A. 1 and 4
B. 2 and 3
C. 3 and 4
D. 1 and 3
6.
x = 0;
if (x1.hashCode() != x2.hashCode() ) x = x + 1;
if (x3.equals(x4) ) x = x + 10;
if (!x5.equals(x6) ) x = x + 100;
if (x7.hashCode() == x8.hashCode() ) x = x + 1000;
System.out.println("x = " + x);
and assuming that the equals() and hashCode() methods are properly implemented, if the
output is "x = 1111", which of the following statements will always be true?
A. x2.equals(x1)
B. x3.hashCode() == x4.hashCode()
C. x5.hashCode() != x6.hashCode()
D. x8.equals(x7)
7.
classBoo
{
Boo(String s) { }
Boo() { }
}
classBarextendsBoo
{
Bar() { }
Bar(String s) {super(s);}
void zoo()
{
// insert code here
}
}
which one create an anonymous inner class from within class Bar?
A. Boo f = new Boo(24) { };
B. It prints "Zippo".
Set-16
1. What will be the output of the program?
publicabstractclassAbstractTest
{
publicint getNum()
{
return45;
}
publicabstractclassBar
{
publicint getNum()
{
return38;
}
}
publicstaticvoid main (String [] args)
{
AbstractTest t = new AbstractTest()
{
publicint getNum()
{
return22;
}
};
AbstractTest.Bar f = t.new Bar()
{
publicint getNum()
{
return57;
}
};
A. 57 22
B. 45 38
C. 45 57
The program prints pairs of values for x and y that might not always be the same
C.
on the same line (for example, "x=2, y=1")
The program prints pairs of values for x and y that are always the same on the
D. same line (for example, "x=1, y=1". In addition, each value appears once (for
example, "x=1, y=1" followed by "x=2, y=2")
3. What will be the output of the program?
classMyThreadextendsThread
{
publicstaticvoid main(String [] args)
{
MyThread t = new MyThread(); /* Line 5 */
t.run(); /* Line 6 */
}
publicvoid run()
{
for(int i=1; i <3; ++i)
{
System.out.print(i + "..");
}
}
}
C. 1..2..
D. 1..2..3..
4. What will be the output of the program?
publicclassThreadTestextendsThread
{
publicvoid run()
{
System.out.println("In run");
yield();
System.out.println("Leaving run");
}
publicstaticvoid main(String []argv)
{
(new ThreadTest()).start();
}
}
A. Compilation error.
B. x.finalize()
C. Runtime.getRuntime().gc()
Private getter() and setter() methods should not use assertions to verify
B.
arguments.
if (x == y) /* Line 13 */
result = 1;
if (x.equals(y) ) /* Line 15 */
result = result + 10;
if (x.equals(z) ) /* Line 17 */
result = result + 100;
if (x.equals(x2) ) /* Line 19 */
result = result + 1000;
if (x.equals(z2) ) /* Line 21 */
result = result + 10000;
A. result = 1
B. result = 10
C. result = 11
D. result = 11010
9. What will be the output of the program?
classQ207
{
publicstaticvoid main(String[] args)
{
int i1 = 5;
int i2 = 6;
String s1 = "7";
System.out.println(i1 + i2 + s1); /* Line 8 */
}
}
A. 18
B. 117
C. 567
D. Compiler error
10. What will be the output of the program?
publicclassStringRef
{
publicstaticvoid main(String [] args)
{
String s1 = "abc";
String s2 = "def";
String s3 = s2; /* Line 7 */
s2 = "ghi";
System.out.println(s1 + s2 + s3);
}
}
A. abcdefghi
B. abcdefdef
C. abcghidef
D. abcghighi
Set-17
1. What will be the output of the program?
classSuper
{
publicint i = 0;
classSubextendsSuper
{
public Sub(String text)
{
i = 2;
}
A. 0
B. 1
C. 2
D. Compilation fails.
2. What will be the output of the program?
classBase
{
Base()
{
System.out.print("Base");
}
}
publicclassAlphaextendsBase
{
publicstaticvoid main(String[] args)
{
new Alpha(); /* Line 12 */
new Base(); /* Line 13 */
}
}
A. Base
B. BaseBase
C. Compilation fails
A. 024
B. 0246
B. 2 and 4
C. 1 and 5
D. 2 and 6
5. What will be the output of the program?
publicclassX
{
publicstaticvoid main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
publicstaticvoid badMethod()
{
thrownew Error(); /* Line 22 */
}
}
A. ABCD
B. Compilation fails.
A. AB
B. BC
C. ABC
D. BCD
7. Which interface does java.util.Hashtable implement?
A. Java.util.Map
B. Java.util.List
C. Java.util.HashTable
D. Java.util.Collection
8. What will be the output of the program?
package foo;
import java.util.Vector; /* Line 2 */
privateclassMyVectorextendsVector
{
int i = 1; /* Line 5 */
public MyVector()
{
i = 2;
}
}
publicclassMyNewVectorextendsMyVector
{
public MyNewVector ()
{
i = 4; /* Line 15 */
}
publicstaticvoid main (String args [])
{
MyVector v = new MyNewVector(); /* Line 19 */
}
}
A. 1 and 2
B. 2 and 3
C. 3 and 4
D. 1 and 3
10. Which three are methods of the Object class?
1. notify();
2. notifyAll();
3. isInterrupted();
4. synchronized();
5. interrupt();
6. wait(long msecs);
7. sleep(long msecs);
8. yield();
A. 1, 2, 4
B. 2, 4, 5
C. 1, 2, 6
D. 2, 3, 4
Set-18
1. Which cannot directly cause a thread to stop executing?
A. Calling the SetPriority() method on a Thread object.
B. construct();
C. start();
D. register();
3.
publicclassMyRunnableimplementsRunnable
{
publicvoid run()
{
// some code here
}
}
B. new Thread(MyRunnable).run();
D. new MyRunnable().start();
4.
void start() {
A a = new A();
B b = new B();
a.s(b);
b = null; /* Line 5 */
a = null; /* Line 6 */
System.out.println("start completed"); /* Line 7 */
}
B. after line 6
C. after line 7
after line 11 runs, how many objects are eligible for garbage collection?
A. 0
B. 1
C. 2
D. 3
6. Which of the following would compile without error?
A. int a = Math.abs(-5);
B. int b = Math.abs(5.0);
C. int c = Math.abs(5.5F);
D. int d = Math.abs(5L);
7. What will be the output of the program?
String x = new String("xyz");
String y = "abc";
x = x + y;
B. 3
C. 4
D. 5
8. What will be the output of the program?
System.out.println(Math.sqrt(-4D));
A. -2
B. NaN
C. Compile Error
D. Runtime Exception
9. What will be the output of the program?
publicclassTest
{
publicstaticvoid main(String[] args)
{
final StringBuffer a = new StringBuffer();
final StringBuffer b = new StringBuffer();
new Thread()
{
publicvoid run()
{
System.out.print(a.append("A"));
synchronized(b)
{
System.out.print(b.append("B"));
}
}
}.start();
new Thread()
{
publicvoid run()
{
System.out.print(b.append("C"));
synchronized(a)
{
System.out.print(a.append("D"));
}
}
}.start();
}
}
A. ACCBAD
B. ABBCAD
C. CDDACB
D. Indeterminate output
10. Which statement is true given the following?
Double d = Math.random();
C. Compilation fail
D. Cannot say.
Set-19
1. What will be the output of the program?
publicclassTest
{
publicint aMethod()
{
staticint i = 0;
i++;
return i;
}
publicstaticvoid main(String args[])
{
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
A. 0
B. 1
C. 2
D. Compilation fails.
2. What will be the output of the program?
classPassA
{
publicstaticvoid main(String [] args)
{
PassA p = new PassA();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
A. 12 15
B. 15 15
C. 3 4 5 3 7 5
D. 3 7 5 3 7 5
3. What will be the output of the program?
classEquals
{
publicstaticvoid main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
System.out.println(b);
}
}
A. true
B. false
C. Compilation fails
A. 0
B. 7
C. 8
D. 14
5. What will be the output of the program?
classSSBool
{
publicstaticvoid main(String [] args)
{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
System.out.print("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
System.out.println("dokey");
}
}
A. ok
B. dokey
C. ok dokey
D. No output is produced
E. Compilation error
6. What will be the output of the program?
int x = l, y = 6;
while (y--)
{
x++;
}
System.out.println("x = " + x +" y = " + y);
A. x=6y=0
B. x=7y=0
C. x = 6 y = -1
D. Compilation fails.
7. What will be the output of the program?
for (int i = 0; i <4; i += 2)
{
System.out.print(i + " ");
}
System.out.println(i); /* Line 5 */
A. 024
B. 0245
C. 0 1 2 3 4
D. Compilation fails.
8. What will be the output of the program?
int x = 3;
int y = 1;
if (x = y) /* Line 3 */
{
System.out.println("x =" + x);
}
A. x=1
B. x=3
C. Compilation fails.
A. BD
B. BCD
C. BDE
D. BCDE
10.
publicclassExceptionTest
{
classTestExceptionextendsException {}
publicvoid runTest() throws TestException {}
publicvoid test() /* Point X */
{
runTest();
}
}
B. throws Exception
C. catch ( Exception e )
D. throws RuntimeException
Set-20
1. You need to store elements in a collection that guarantees that no duplicates are stored. Which
one of the following interfaces provide that capability?
A. Java.util.Map
B. Java.util.List
C. Java.util.Collection
B. java.util.TreeMap
C. java.util.TreeSet
D. java.util.Hashtable
3. What is the numerical range of char?
A. 0 to 32767
B. 0 to 65535
C. -256 to 255
D. -32768 to 32767
4. Which of the following are true statements?
1. The Iterator interface declares only three methods: hasNext, next and remove.
2. The ListIterator interface extends both the List and Iterator interfaces.
3. The ListIterator interface provides forward and backward iteration capabilities.
4. The ListIterator interface provides the ability to modify the List during iteration.
5. The ListIterator interface provides the ability to determine its position in the List.
A. 2, 3, 4 and 5
B. 1, 3, 4 and 5
C. 3, 4 and 5
D. 1, 2 and 3
5. Which method must be defined by a class implementing the java.lang.Runnable interface?
A. void run()
B. 3 and 5
C. 4 and 6
D. 1 and 3
7. Which of the following statements is true?
If assertions are compiled into a source file, and if no flags are included at runtime,
A.
assertions will execute by default.
A. 9.0
B. bad number
A. 1
B. 2
C. 3
D. Compilation Fails
10. What will be the output of the program?
String s = "hello";
Object o = s;
if( o.equals(s) )
{
System.out.println("A");
}
else
{
System.out.println("B");
}
if( s.equals(o) )
{
System.out.println("C");
}
else
{
System.out.println("D");
}
1. A
2. B
3. C
4. D
A. 1 and 3
B. 2 and 4
C. 3 and 4
D. 1 and 2
C++ Questions
Set-1
1. Which constructor function is designed to copy objects of the same class type?
A. Create constructor
B. Object constructor
C. Dynamic constructor
D. Copy constructor
2. What happens if the base and derived class contains definition of a function with same
prototype?
A. Compiler reports an error on compilation.
Base class object will call base class function and derived class object will call
D.
derived class function.
3. Which of the following term is used for a function defined inside a class?
A. Member Variable
B. Member function
C. Class function
D. Classic function
4. Why reference is not same as a pointer?
A. A reference can never be null.
A. class
B. member functions
C. constructor
D. destructor
6. What will happen if a class is not having any name?
A. It cannot have a destructor.
C. It is not allowed.
D. Both A and B.
7. Which of the following statement is correct about the program given below?
#include<iostream.h>
class IndiaBix
{
int x;
public:
IndiaBix()
{
x = 0;
}
IndiaBix(int xx)
{
x = xx;
}
IndiaBix(IndiaBix &objB)
{
x = objB.x;
}
void Display()
{
cout<< x <<" ";
}
};
int main()
{
IndiaBix objA(25);
IndiaBix objB(objA);
IndiaBix objC = objA;
objA.Display();
objB.Display();
objC.Display();
return0;
}
A. Copy constructor
B. Simple constructor
C. Non-parameterized constructor
D. Default constructor
9. Which of the following statement is correct about the program given below?
#include<iostream.h>
class IndiaBix
{
public:
IndiaBix()
{
cout<<"India";
}
~IndiaBix()
{
cout<<"Bix";
}
};
int main()
{
IndiaBix objBix;
return0;
}
B. Friend keyword can be used for a function in the public section of a class.
C. Friend keyword can be used for a function in the private section of a class.
Set-2
1. Which of the following statement is correct?
A. Constructors can have default parameters.
A. 2
B. 3
C. 5
D. 8
3. Which of the following statement is correct about the program given below?
#include<iostream.h>
class IndiaBix
{
staticint x;
public:
staticvoid SetData(int xx)
{
x = xx;
}
staticvoid Display()
{
cout<< x ;
}
};
int IndiaBix::x = 0;
int main()
{
IndiaBix::SetData(44);
IndiaBix::Display();
return0;
}
A. 45 22
B. 46 22
C. 45 23
D. 46 23
6. Which of the following statements is correct?
1. Pointer to a reference and reference to a pointer both are valid.
2. When we use reference, we are actually referring to a referent.
A. Only 1 is correct.
B. Only 2 is correct.
B. Only 2 is correct.
A. 09365
B. 93650
C. 5 6 3 9 0
D. 5 9 3 6 0
10. Which of the following statement is correct about the program given below?
#include<iostream.h>
class BixArray
{
int array[3][3];
public:
BixArray(int arr[3][3] = NULL)
{
if(arr != NULL)
for(int i = 0; i <3; i++)
for(int j = 0; j <3; j++)
array[i][j] = i+j;
}
void Display(void)
{
for(int i = 0; i <3; i++)
for(int j = 0; j <3; j++)
cout<< array[i][j] <<" ";
}
};
int main()
{
BixArray objBA;
objBA.Display();
return0;
}
Set-3
1. Which of the following concepts means adding new components to a program as it runs?
A. Data hiding
B. Dynamic typing
C. Dynamic binding
D. Dynamic loading
2. Which of the following statement is correct?
A. C++ allows static type checking.
D. Both A and B.
3. Which of the following statement is correct?
A. Only one parameter of a function can be a default parameter.
A. 0
B. 5
C. 100
D. -5
E. None of these
7. Which of the following statement is correct about the program given below?
#include<iostream.h>
void Tester(float xx, float yy = 5.0);
class IndiaBix
{
float x;
float y;
public:
void Tester(float xx, float yy = 5.0)
{
x = xx;
y = yy;
cout<< ++x % --y;
}
};
int main()
{
IndiaBix objBix;
objBix.Tester(5.0, 5.0);
return0;
}
};
int main()
{
IndiaBix objIB;
objIB.BixFunction(15.09, 'A', char('A' + 'A'));
return0;
}
void IndiaBix::BixFunction(float, char y, char z)
{
K = int(z);
K = int(y);
K = y + z;
cout<<"K = "<< K << endl;
}
A. 0
B. 314
C. 314.0000
D. 200.0000
Set-4
1. Which of the following statements is correct?
1. We can return a global variable by reference.
2. We cannot return a local variable by reference.
A. Only 1 is correct.
B. Only 2 is correct.
C. Both 1 and 2 are correct.
B. One
C. Two
D. As many as we want
4. Which of the following statement is correct about the program given below?
#include<iostream.h>
class IndiaBix
{
staticint x;
public:
staticvoid SetData(int xx)
{
x = xx;
}
staticvoid Display()
{
cout<< x ;
}
};
int IndiaBix::x = 0;
int main()
{
IndiaBix::SetData(44);
IndiaBix::Display();
return0;
}
A. 45 22
B. 46 22
C. 45 23
D. 46 23
6. What will be the output of the following program?
#include<iostream.h>
class Point
{
int x, y;
public:
Point(int xx = 10, int yy = 20)
{
x = xx;
y = yy;
}
Point operator + (Point objPoint)
{
Point objTmp;
objTmp.x = objPoint.x + this->x;
objTmp.y = objPoint.y + this->y;
return objTmp;
}
void Display(void)
{
cout<< x <<" "<< y;
}
};
int main()
{
Point objP1;
Point objP2(1, 2);
Point objP3 = objP1 + objP2;
objP3.Display();
return0;
}
A. 12
B. 10 20
C. 11 22
D. Garbage Garbage
C. are constructed
D. are destroyed
8. Copy constructor must receive its arguments by __________ .
A. either pass-by-value or pass-by-reference
B. only pass-by-value
C. only pass-by-reference
B. Linker
C. Loader
D. Compiler
10. Which of the following statement is correct?
A constructor of a derived class can access any public and protected member of
A.
the base class.
B. Constructor cannot be inherited but the derived class can call them.
A constructor of a derived class cannot access any public and protected member of
C.
the base class.
D. Both A and B.
Set-5
1. Which of the following is not a type of constructor?
A. Copy constructor
B. Friend constructor
C. Default constructor
D. Parameterized constructor
2. Which of the following is an abstract data type?
A. int
B. double
C. string
D. Class
3. Which of the following approach is adapted by C++?
A. Top-down
B. Bottom-up
C. Right-left
D. Left-right
4. Which of the following functions are performed by a constructor?
A. Construct a new class
D. Initialize objects
5. In which of the following a virtual call is resolved at the time of compilation?
A. From inside the destructor.
D. Both A and B.
6. Which of the following statements is correct in C++?
A. Classes cannot have data as protected members.
A. 24
B. 68
Set-6
11. Which of the following statement is correct about the program given below?
#include<iostream.h>
int main()
{
int x = 80;
int&y = x;
x++;
cout<< x <<" "<< --y;
return0;
}
A. 236
B. 447
C. 4 5 8
D. 3 4 6
13. Which of the following statement is correct regarding destructor of base class?
A. Destructor of base class should always be static.
A. 2
B. 3
C. 5
D. 8
15. A function with the same name as the class, but preceded with a tilde character (~) is called
__________ of that class.
A. constructor
B. destructor
C. function
D. object
16. Which of the following statements are correct?
A. Constructor is always called explicitly.
D. Constructor and destructor functions are not called at all as they are always inline.
17. How many times a constructor is called in the life-time of an object?
A. Only once
B. Twice
C. Thrice
D. Depends on the way of creation of object
18. Which of the following constructor is used in the program given below?
#include<iostream.h>
class IndiaBix
{
int x, y;
public:
IndiaBix(int xx = 10, int yy = 20 )
{
x = xx;
y = yy;
}
void Display()
{
cout<< x <<" "<< y << endl;
}
~IndiaBix()
{ }
};
int main()
{
IndiaBix objBix;
objBix.Display();
return0;
}
A. Copy constructor
B. Simple constructor
C. Non-parameterized constructor
D. Default constructor
19. Which of the following statement is correct about the program given below?
#include<iostream.h>
class IndiaBix
{
int x;
public:
IndiaBix(short ss)
{
cout<<"Short"<< endl;
}
IndiaBix(int xx)
{
cout<<"Int"<< endl;
}
IndiaBix(float ff)
{
cout<<"Float"<< endl;
}
~IndiaBix()
{
cout<<"Final";
}
};
int main()
{
IndiaBix *ptr = new IndiaBix('B');
return0;
}
Set-7
1. Which of the following factors supports the statement that reusability is a desirable feature of a
language?
A. It decreases the testing time.
D. Both A and B.
2. Which of the following is a mechanism of static polymorphism?
A. Operator overloading
B. Function overloading
C. Templates
Base class object will call base class function and derived class object will call
D.
derived class function.
4. Which of the following is not a type of inheritance?
A. Multiple
B. Multilevel
C. Distributive
D. Hierarchical
5. Which of the following keyword is used to overload an operator?
A. overload
B. operator
C. friend
D. override
6. Which of the following statement is correct?
A. Class is an instance of object.
B. Static function
C. Virtual function
D. Both B and C
8. Which of the following statement is incorrect?
A. The default value for an argument can be a global constant.
C. Compiler uses the prototype information to build a call, not the function definition.
The default arguments are given in the function prototype and should be repeated
D.
in the function definition.
9. What will be the output of the following program?
#include<iostream.h>
int BixFunction(int a, int b = 3, int c = 3)
{
cout<< ++a * ++b * --c ;
return0;
}
int main()
{
BixFunction(5, 0, 0);
return0;
}
A. 8
B. 6
C. -6
D. -8
10. What will be the output of the following program?
#include<iostream.h>
struct MyData
{
public:
int Addition(int a, int b = 10)
{
return (a *= b + 2);
}
float Addition(int a, float b);
};
int main()
{
MyData data;
cout<<data.Addition(1)<<" ";
cout<<data.Addition(3, 4);
return0;
}
A. 12 12
B. 12 18
C. 3 14
D. 18 12
E. Compilation fails.
Set-8
1. Which of the following statements is correct?
1. Once the variable and the reference are linked they are tied together.
2. Once the reference of a variable is declared another reference of that variable is not allowed.
A. Only 1 is correct.
B. Only 2 is correct.
Once a reference variable has been defined to refer to a particular variable it can
B.
refer to any other variable.
B. Inheritance
C. Composition
D. Abstraction
7. Destructor has the same name as the constructor and it is preceded by ______ .
A. !
B. ?
C. ~
D. $
8. When are the Global objects destroyed?
A. When the control comes out of the block in which they are being used.
C. When the control comes out of the function in which they are being used.
B. Two
C. Three
D. Unlimited
10. Which of the following statement is correct about destructors?
A. A destructor has void return type.
Set-9
1. Which of the following type of class allows only one object of it to be created?
A. Virtual class
B. Abstract class
C. Singleton class
D. Friend class
2. How many instances of an abstract class can be created?
A. 1
B. 5
C. 13
D. 0
3. Why reference is not same as a pointer?
A. A reference can never be null.
A. istream.h
B. ostream.h
C. iomanip.h
D. iostream.h
6. Which of the following provides a reuse mechanism?
A. Abstraction
B. Inheritance
C. Dynamic binding
D. Encapsulation
7. Which of the following statement will be correct if the function has three arguments passed to it?
A. The trailing argument will be the default argument.
C. Overloaded functions can accept only same number and same type of arguments.
Overloaded functions can accept only different number and different type of
D.
arguments.
9. Which of the following function / types of function cannot have default parameters?
A. Member function of class
B. main()
D. Both B and C
10. What will be the output of the following program?
#include<iostream.h>
struct IndiaBix
{
int arr[5];
public:
void BixFunction(void);
void Display(void);
};
void IndiaBix::Display(void)
{
for(int i = 0; i <5; i++)
cout<< arr[i] <<" " ;
}
void IndiaBix::BixFunction(void)
{
staticint i = 0, j = 4;
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp ;
i++;
j--;
if(j != i) BixFunction();
}
int main()
{
IndiaBix objBix = {{ 5, 6, 3, 9, 0 }};
objBix.BixFunction();
objBix.Display();
return0;
}
A. 09365
B. 93650
C. 5 6 3 9 0
D. 5 9 3 6 0
Set-10
1. What is correct about the following program?
#include<iostream.h>
class Base
{
int x, y, z;
public:
Base()
{
x = y = z = 0;
}
Base(int xx, int yy = 'A', int zz = 'B')
{
x = xx;
y = x + yy;
z = x + y;
}
void Display(void)
{
cout<< x <<" "<< y <<" "<< z << endl;
}
};
class Derived : public Base
{
int x, y;
public:
Derived(int xx = 65, int yy = 66) : Base(xx, yy)
{
y = xx;
x = yy;
}
void Display(void)
{
cout<< x <<" "<< y <<" ";
Display();
}
};
int main()
{
Derived objD;
objD.Display();
return0;
}
D. The program will run successfully giving the output 66 65 65 131 196.
The program will produce the output 66 65 infinite number of times (or till stack
E.
memory overflow).
2. Which of the following statement is correct?
A. A reference is declared using * operator.
Once a reference variable has been defined to refer to a particular variable it can
B.
refer to any other variable.
C. Member Functions
D. Member Variables
6. Constructor is executed when _____.
A. an object is created
B. an object is used
C. a class is declared
B. destructor
C. main
D. virtual function
8. It is a __________ error to pass arguments to a destructor.
A. logical
B. virtual
C. syntax
D. linker
9. Which of the following gets called when an object is being created?
A. constructor
B. virtual function
C. destructor
D. main
10. Which of the following statement is correct about the program given below?
#include<iostream.h>
class IndiaBix
{
int x;
public:
IndiaBix(short ss)
{
cout<<"Short"<< endl;
}
IndiaBix(int xx)
{
cout<<"Int"<< endl;
}
IndiaBix(char ch)
{
cout<<"Char"<< endl;
}
~IndiaBix()
{
cout<<"Final";
}
};
int main()
{
IndiaBix *ptr = new IndiaBix('B');
return0;
}