C Questions
C Questions
int x=2;
switch(x)
default:printf(“CDC”);
}
a. BDC
b. BDCMDCCDC
c. BDCMDC
d. CDC
2. Which of the following is a properly defined struct?
A. struct {int a;}
B. struct a_struct {int a;}
C. struct a_struct int a;
D. struct a_struct {int a;};
char *p;
p = (char*) malloc(100);
A.char p = *malloc(100);
B.char *p = (char) malloc(100);
C.char *p = (char*)malloc(100);
char *p = (char *)(malloc*)(100);
D.
What will be the output of the program ?
#include<stdio.h>
int main()
{
int i=3, *j, k;
j = &i;
printf("%d\n", i**j*i+*j);
return 0;
}
A.30 B.27
C.9 D.3
#include<stdio.h>
int main()
{
char str[20] = "Hello";
char *const p=str;
*p='M';
printf("%s\n", str);
return 0;
}
A
Mello B.Hello
.
C D
HMello MHello
. .
Algorithm:
1. Begin
5. End
a) Sequential algorithm
b) Single Alternative algorithm
c) Multiple alternative algorithm
d) Algorithm Iteration
for(loop=0;loop<100;loop++)
if (loop==50)
continue;
printf("%d\n",loop);
int a[ ] = {1,2,3,4};
a) 0
b) 4
c) Array size should be specified
d) 5
13) What would be the size of the character array to store a string NEWYORK
1) 7
2) 8
3) 9
4) 6
if (i>7)
break;
else
printf(“%d”,i);
a) 01234567
b) 01234567I is greater than 7
c) 0123456
d) 0123456I is greater than 7
Str1=Computer
Str2=Computer
18) When we use strcmp(Str1,Str2) which value will be returned
a) 0
b) Positive value
c) Negative value
d) None of the above
a) 0
b) 1
c) 3
d) 4
void main()
{
clrscr();
increment();
increment();
increment();
getch();
}
increment()
{
static int i=1;
printf("%d",i);
i=i+1;
}
a) 111
b) 123
c) 222
d) None of the above
void main()
{
clrscr();
increment();
increment();
increment();
}
increment()
{
auto int i=1;
printf("%d",i);
i=i+1;
}
a) 111
b) 123
c) 222
d) None of the above
#include<stdio.h>
int main()
{
int a=0;
#if (a==0)
printf("Equal");
#else if
printf("Not equal");
#endif
return 0;
}
a) Equal
b) Not equal
c) Null
d) Compilation error
Explanation:
In this code (a==0) is not constant expression. A constant expression mean expression doesn’t contain
any variables.
Note: const int a; Here a is also variable
#include<stdio.h>
int main(){
for(;NULL;)
printf("cquestionbank");
return 0;
}
a) c
b) cquestionbank
c) Infinite loop
d) Compilation error
Here NULL is micro constantan. Value of this symbolic constant is 0 or 0L as defined stdio.h:
int main(){
for(;0;)
printf("cquestionbank");
return 0;
}
As you know in c :
0: Means false
Non- zero: True
So for loop should not execute any time because intitial condtion is false. But it is bug of turbo c compiler.
Loop will execute one time and it will print : cquestionbank
#include<stdio.h>
int main(){
int x=25;
if(!!x)
printf("%d",!x);
else
printf("%d",x);
return 0;
}
a) 0
b) 25
c) 1
d) -1
! is negation operator.
!x = 0 if x is non-zero number.
!x = 1 if x is equal to zero.
So,
!!x
=! (! x)
=! (! 25)
=! (0)
=1
As we know in c:
Zero: It represents false.
Non-zero: It represents false.
if (1) means condition is true hence if part will execute.
!x =! 1 = 0
Hence printf statement will print zero in the console window.
#include<stdio.h>
int main(){
float a=0.5, b=0.9;
if(a&&b>0.9)
printf("Sachin");
else
printf("Rahul");
return 0;
}
a) Sachin
b) Rahul
c) Null
d) Run time error
28) What will be output if you will execute following c code?
#include<stdio.h>
int main(){
int x=5, y=10;
if(!(!x) && x)
printf("%d",x);
else
printf("%d",y);
return 0 ;
}
a) 1
b) 5
c) 10
d) Compilation error
Consider on expression:
! (! x) && x
=! (! 5) && 5
=! 0 && 5
=1 && 5
=1
So, if condition is true.
#include<stdio.h>
int main(){
int x=3, y=4, z=4;
printf("%d", (z>=y>=x?100:200));
return 0 ;
}
a) 100
b) 200
c) 0
d) Compilation Error
a) deallocate()
b) release()
c) free()
d) drop()
31) Which function is used to read a float data using scanf() in an array of structures
a) float()
b) linkfloat()
c) floatlink()
d) No function is required.
a) 7
b) 4
c) 5
d) 11
#include <stdio.h>
void inc_ptr(*int p)
{
(*p)++;
return;
}
int main()
{
int *p;
*p=12;
inc_ptr(p);
printf("%d", *p);
return 0;
}
a) 12
b) 13
c) Undefined
The pointer p is uninitialized and points to a random location in memory. The statement *p=12
will simply try to write a value 12 to whatever random location p points to.
34) If the program completes executing successfully, what value should the function main()
return?
a) -1
b) 0
c) 1
d) Void
35) gets and puts function are define in the following header file.
a) Option A: string.h
b) Option B: stdio.h
c) Option C: iostream.h
d) Option D: math.h
a) Option A: stdio.h
b) Option B: string.h
c) Option C: conio.h
d) Option D: iostream.h
main()
printf("%d", c--);
if(c)
main();
a) Option A: 54321
b) Option B: 4321
c) Option C: No Output
38) Malloc() function used in dynamic allocation is available in which header file?
a) Option A: Stdio.h
b) Option B: Stdlib.h
c) Option C: Conio.h
d) Option D: Mem.h
main()
int k=35, z;
k=func1(k=func1(k=func1(k)));
printf("k=%d", k);
func1()
int k;
k++;
return(k);
a) Option A: 36
b) Option B: 37
c) Option C: 38
d) Option D: 39
41) What will the above sample code produce when executed?
void myFunc (int x)
{
if (x > 0)
myFunc(--x);
printf("%d, ", x);
}
int main()
{
myFunc(5);
return 0;
}
a) 1, 2, 3, 4, 5, 5,
b) 4, 3, 2, 1, 0, 0,
c) 0, 0, 1, 2, 3, 4,
d) 0, 1, 2, 3, 4, 5,
1) 2
2) 3
3) 4
4) 5
a) True
b) False
char *getptr()
{
static char ptr[10] = "123456789";
return ptr;
}
main()
{
char *ptr="00000";
strcpy(getptr()+4,ptr);
ptr=getptr();
5[strcpy(ptr,"12345")]='6';
printf("The string is : %s",getptr());
}
a) 123456789
c) 123456000
d) 12345
char **p;
p=s;
printf("%s ",++*p);
printf("%s ",*p++);
printf("%s ",++*p);
int cnt = 5, a;
do {
a /= cnt;
a) Zero
b) 1
a) printf("%sabc\\n", A);
b) strcmp(A, "abc");
c) strstr(A, "abc");
d) strchr(A, "abc");
a) compare();
b) stringcompare();
c) cmp()
d) strcmp()
49) The sentence "Hello world!" uses _____ elements in a character array.
a) 11
b) 12
c) 13
d) 14
50) Which of the following adds one string to the end of another?
a) append();
b) stringadd();
c) strcat();
d) stradd();
51) What does the variable i contain after the following code executes?
int i = 17;
int *p = &i;
*p = 98;
a) 98
b) 17
c) 81
d) 0
54) What is the difference between the following two chunks of code?
char arr1[] = {'a', 'b', 'c'};
and
char *arr2 = "abc";
#include <stdio.h>
void inc_ptr(*int p){
(*p)++;
return;
}
int main(){
int *p;
*p=12;
inc_ptr(p);
printf("%d", *p);
return 0;
}
a)12
b)13
c) undefined
56) Let p be a pointer to an integer and i be an integer variable. Which of the following is
not a correct assignment?
(A) p = 0;
(B) p = i;
(C) p = p + 1;
(D) p = &i;
(A) i = 5, j = 10
(B) i = 5, j = 5
(C) i=10, j = 5
(D) Nothing. The program will most likely crash.
58) int *x; and int* x; both have the same effect.
(A) True
(B) False
59) A dangling pointer is a pointer which points to a thing in memory which has been
free'd or has vanished for some other reason. Dangling pointers are bad because you
don't know what's at that spot any longer. In the following code, what is the dangling
pointer?
int* y = malloc(sizeof(int));
int* z = y;
free(z);
(A) y
(B) z
(C) Both x and y
(A) 23 and 23
(B) 23 and 72
(C) 72 and 23
(D) 72 and 72
a)I love u
b)I hate u
c)Error
d)None of the above
Explanation: For floating point numbers(float, double, long double) the values cannot be
predicted exactly. Depending on the number of bytes, the precession with of the value
represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with
less precision than long double.
Rule of Thumb: Never compare or at-least be cautious when using floating point numbers with
relational operators(== , >, <, <=, >=,!=)
a)11,11
b)Cannot increment a void pointer
c)10,10
d)10,11
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {"Tiger"};
printf ("\n%d %f", e.age, e.sal) ;
}
A. 0 0.000000
B. Garbage values
C. Error
D. None of the above
main() {
float a = 0.7; if (a < 0.7) printf ( " C " ) ;
else
printf ( " C + + " ) ;
}
A. C
B. C++
C. Error
D. None of the above
main( )
{
int x, j, k;
j=k=6; x=2;
x=j*k;
printf("%d", x);
}
a)6
b)36
c) 2
d)Error
main( )
{
int a=0;
if(a=0) printf("Ramco Systems\n");
printf("Ramco Systems\n");
}
a) Ramco Systems will be printed twice
b) Ramco Systems will be printed once
c) Compilation Error
d) None of the above
main()
char t1[]=“Exforsys”;
printf(“%s”,t1);
a) E
b) Exforsys
c). NULL
main()
int a=10;
int b;
b=a++ + 20;
printf("a=%d b=%d",a,b);
b) a=11 b=30
c) a=10 b=30
d) a=11 b=31
77) How many while statements are possible in do.... While loop?
A. 2
B. 3
C. any number
D. 1
87)The amount of storage required for holding elements of the array depends on
A. data type
B. datatype and size
C. run-time requirement
D. size
90)
count=0;
for ( I=0;I<=10; I++)
{if(I%2==0)
count++;
}printf(“%d”, count);
Pick out the correct value for count
A. 6
B. 3
C. 4
D. 5
91) What will happen if you try to put so many values into an array during the initialization such
that its size is exceeded
A. 28
B. 10
C. 15
D. 21
94) Which of the following is a correct way of defining a symbolic constant pie in C
A. # define pie = 22/7
B. #define pie 22/7
C. #define pie= 3.142
D. # Define pie 22/7
95) The minimum number of times the for loop is executed is
A. 0
B. cannot be predicted
C. 1
D. 2
101)
switch(ch)
{
case ‘a’: printf(“a”);
case ‘b’: printf(“b”);
default: printf(“error”);
}
if ch is assigned to the character ‘a’ then the output will be
A. a b
B. a b error
C. a
D. error
102)
main( )
{
float a;
int x = 6; y = 4;
a = x/y;
print (“%f”,a)
}
what is the output
A. error
B. 1.5
C. 0.5
D. 1.00
103) The output of the following program is
main( )
{
int i=2;
printf(“%d %d %d”,i++,i,++i);
}
A. 2 2 4
B. 2 3 3
C. 3 3 3
D. 2 3 4
104) Size of (double) returns———
A. 8
B. 2
C. 10
D. 4
105) The function -------------------echoes the character typed on the screen
A. getchar()
B. gets()
C. getche()
D. getchr()
106) The purpose of the following fragment
B=S+B
S=B-S;
B=B-S;
Where S,B are two integers is to
A. negate the contents of S and B
B. swap the contents of S and B
C. transfer the contents of S to B
D. transfer the contents of B to S
110)
main( )
{
int a=0;
if(a)
printf(“%d”,++a);
else
printf(“%d”, a+=2) ;
}
the output is
A. 3
B. 1
C. 2
D. 0