0% found this document useful (0 votes)
135 views

C Questions

The document contains questions about C programming concepts like data types, operators, functions, loops, arrays, pointers, structures and more. Some key points: 1. The first question is about a switch statement - the output would be "BDC" as the code falls through each case. 2. Structures must define the data types of members to be properly defined. 3. The ctype.h header file is needed for typecasting. 4. The last few questions cover pointers, memory allocation, loops, conditional statements and their outputs for various programs.

Uploaded by

abhiram
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views

C Questions

The document contains questions about C programming concepts like data types, operators, functions, loops, arrays, pointers, structures and more. Some key points: 1. The first question is about a switch statement - the output would be "BDC" as the code falls through each case. 2. Structures must define the data types of members to be properly defined. 3. The ctype.h header file is needed for typecasting. 4. The last few questions cover pointers, memory allocation, loops, conditional statements and their outputs for various programs.

Uploaded by

abhiram
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

1. What is the result of the following code?

int x=2;

switch(x)

case 1: printf( "HDC" );

case 2: printf( "BDC" );

case 3: printf( "MDC" );

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;};

3. Which header file do you need to include to use typecasting?


A. string.h
4. B. ctype.h
C. math.h
D. None
5.

Can you combine the following two statements into one?

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

6) What will be the output of the program ?

#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:

7) The order of C compilation model

1) PreprocessorCompilerAssembler Link editor


2) Compiler Preprocessor Link editor Assembler
3) AssemblerCompiler Preprocessor Link editor
4) Assembler Link editor PreprocessorCompiler

8) The below algorithm is an example for

1. Begin

2. Print “Enter Marks of 3 subjects”

3. Input marks1, marks2,marks3


Avg=marks1+marks2+marks3)/3

4. If (Avg >= 50) Then


Print “Passed”
Else
Print “Failed”

5. End
a) Sequential algorithm
b) Single Alternative algorithm
c) Multiple alternative algorithm
d) Algorithm Iteration

9) What is the output of the following program:


void main(){
    if(printf("Hello world")){
    }
}

a) Hello World b)Error C)None of the above d)1

10) What is the output of the program

for(loop=0;loop<100;loop++)

if (loop==50)

continue;

printf("%d\n",loop);

a) The numbers 0 through 99 are printed except 50.

b) The numbers 0 through 99 are printed


c) Error

d) The numbers 0 through 100 are printed

11) What is the size of the Array

int a[ ] = {1,2,3,4};

a) 0
b) 4
c) Array size should be specified
d) 5

12) Which value will be taken by a[4] element


Int a[5]={1,2,3,4}
a) 0
b) Value should be specified
c) 4
5) None of the above

13) What would be the size of the character array to store a string NEWYORK
1) 7
2) 8
3) 9
4) 6

14) The above diagram represents which flow control


a) Sequential
b) Selection
c) Iteration
d) None of the above

15) The above diagram represents which flow control


a) Sequential
b) Selection
c) Iteration
d) None of the above

16) The above diagram represents which flow control


a) Sequential
b) Selection
c) Iteration
d) None of the above

17) What is the output of the below program

for(i=0; i<=10; i++)

if (i>7)

printf(“ I is greater than 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

int b[2][2] = {{1}, {3, 4}};

19) Which value will be stored in b[0][1] element

a) 0
b) 1
c) 3
d) 4

20) What is the output of the below program:


int x;
void main()
{
x=10;
printf("x=%d\n",fun2());
getch();
}
fun2()
{
int x;
x=1;
return(x);
}
a) 10
b) 1
c) 0
d) None of the above

21) What is the output of the below program:


void main()
{
auto int i,j;
clrscr();
printf("i=%d j=%d",i,j);
getch();
}
a) Garbage values
b) 11
c) 00
d) None of the above
22) What is the output of the below program:

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

23) What is the output of the below program:

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

24) What is the output of the program

#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:

Syntax of conditional preprocessor directive (if) is:

#if <Constant expression>


#else
#endif

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     

25) What will be output if you will execute following c code?

#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:

So corresponding intermediate file of above code will be:

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

26) What will be output if you will execute following c code?

#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.

27) What will be output if you will execute following c code?

#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.

29) What will be output if you will execute following c code?

#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

30) What function is used to release the allocated memory space?

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.

32) How bytes will be allocated to the below union


union example
{
char ch
int integer;
float floating_numbers;
}

a) 7
b) 4
c) 5
d) 11

33) What will be the output of this C language program?

#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

36) getch() is defined in the following header file.

a) Option A: stdio.h

b) Option B: string.h

c) Option C: conio.h

d) Option D: iostream.h

37) What is the output of the below program

main()

static int c=5;

printf("%d", c--);

if(c)
main();

a) Option A: 54321

b) Option B: 4321

c) Option C: No Output

d) Option D: Error Message

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

39) What is the output of the below program

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

40) what will be the output when following code is executed


int a=10,b;
b=a++ + ++a;
printf("%d,%d,%d,%d",b,a++,a,++a);
a) 22,10,11,13
b) 22,11,11,11
c) 12,11,11,11
d) 22,13,13,13

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,

42) What is the value of myArray[1][2]; in the sample code above?


int i,j;
int ctr = 0;
int myArray[2][3];
for (i=0; i<3; i++)
for (j=0; j<2; j++)
{
myArray[j][i] = ctr;
++ctr;
}

1) 2
2) 3
3) 4
4) 5

43) In C arrays and pointers can always be used interchangeably.

a) True
b) False

44) What is the output

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

b) run time error

c) 123456000

d) 12345

45) What is the output


void main()

char *s[]={ "miller","miller","filler","filler"};

char **p;

p=s;

printf("%s ",++*p);

printf("%s ",*p++);

printf("%s ",++*p);

a) iller miller iller


b) iller miller filler

c) iller iller filler

d) iller iller iller

46) What will be output of the following program ?


int main()

int cnt = 5, a;

do {

a /= cnt;

} while (cnt --);

printf ("%dn", a);

a) Zero

b) 1

c) run time error

d) insufficient data. value cannot be determined

47) Which of the following finds if the string a contains "abc"?

a) printf("%sabc\\n", A);

b) strcmp(A, "abc");

c) strstr(A, "abc");

d) strchr(A, "abc");

48) Which of the following functions compares two strings?

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

52) What will the following do:


char * str = "stark";
str[1] = 'p';

a) Will not compile.


b) Will change the memory pointed to by str to be "spark".

53) The expression sizeof(int) refers to


a) a program tries to access a memory location forbidden by the operating system.
b) the number of int variables declared in the current function.
c) the number of bytes needed to store an int variable.
d) the largest number of characters allowed in the name of an int variable.

54) What is the difference between the following two chunks of code?
char arr1[] = {'a', 'b', 'c'};
and
char *arr2 = "abc";

a) They are the same.


b) arr2 has a trailing ''\0'' character than arr1 does not.
c) printf("%s", (char *) arr2); will cause a memory violation.

55) What will be the output of this C language program?

#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;

57) What is the output of the following program?


int main ()
{
int i, j, *p, *q;
p = &i;
q = &j;
*p = 5;
*q = *p + i;
printf("i = %d, j = %d\n", i, j);
return 0;
}

(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

60) After the following code:


int i = 23;
int j = 72;
int *p1;
int *p2;
p1 = &i;
p2 = &j;
*p1 = *p2;

what are the values of i and j, respectively?

(A) 23 and 23
(B) 23 and 72
(C) 72 and 23
(D) 72 and 72

61) What is the output of the below program


void main()
{
int const * p=5;
printf("%d",++(*p));
}
a)Compiler Error
b)6
c)5
d)None of the above

62) What would be the output of the program


main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}

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(== , >, <, <=, >=,!=)

63) What is the output of the below program


main()
{
int i=3;
switch(i)
{
default:printf("zero");
case 1: printf("one");
break;
case 2:printf("two");
break;
case 3: printf("three");
break;
}
}
a) three
b) zero
c) Default case should be placed at the end
d) None of the above

64) What is the out of the below program


main()
{
char a[4]="HELLO";
printf("%s",a);
}
a)HELLO
b)HELL
c)Compiler Error
d)None of the above

65) What is the output of the below program


main()
{
int a=10,*j;
void *k;
j=k=&a;
j++;
k++;
printf("\n %u %u ",j,k);
}

a)11,11
b)Cannot increment a void pointer
c)10,10
d)10,11

66) What would be the output of the following program


Main()
{

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

67) What would be the output of the following program?

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

68) What would be the output of the following program?


main()
{
printf ("%d %d %d", sizeof (3.14f), sizeof (3.14), sizeof (3.141));
}
A. 444
B. 4 Garbage value Garbage value
C. 4 8 10
D. Error
69) Find the output for the following C program

main( )
{
int x, j, k;
j=k=6; x=2;
x=j*k;
printf("%d", x);
}

a)6
b)36
c) 2
d)Error

70) Find the output for the following C program

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

71) What is the output of the following program?


main()
{
     int a=10,b=25;
     a=b++ + a++;
     b= ++b + ++a;
     printf(“%d%dn”,a,b);
}
a)    35 65                 
b)  36 64                        
c)  37 64                        
d)  34 64
72) What is the output of the following program?

main()

char t1[]=“Exforsys”;

char t2[]= “Training”;

printf(“%s”,t1);

a) E

b) Exforsys

c). NULL

d). Exforsys Training

73) What is the output of the following C program?

main()

int a=10;

int b;

b=a++ + 20;

printf("a=%d b=%d",a,b);

a) None of the Above

b) a=11 b=30

c) a=10 b=30

d) a=11 b=31

74) Which of the following statements is false

A. The initialization and increment parts of a for statement can be empty


B. The body of do-while statement can be empty
C. The expression in the condition part of a for statement can be empty
D. The initialization part of a for statement cannot have more than one initialization

75) Consider the segment


If(1) printf(“yes”);
else printf(“no”);
what will be the output
A. no
B. Unpredictable
C. yes
D. Error

76) In switch statement


A. more than one default allowed
B. default case, if used, should be the last case
C. default case, if used, can be placed anywhere
D. default case must be present

77) How many while statements are possible in do.... While loop?
A. 2
B. 3
C. any number
D. 1

78) The global variable can be declared


A. after main
B. after block
C. before main
D. within block

79) Which of the following is not correct

A. while loop is executed atleast once


B. do . while loop is executed at least once
C. while loop is executed only if the condition is true
D. dowhile loop is ececuted only if the condition is true
80) printf (“\ “ well done\” ”); what will be the output of this statement
A. \“well done \”
B. well done
C. “ well done”
D. \ well done \

81) The number of elements in array declaration


A. dynamically identifies based on largest index used in program
B. does not require to be specified
C. assume default size as ‘0’
D. requires to be specified

82) All the elements in the array must be


A. defined
B. neither initialized nor defined
C. initialized and defined
D. initialized
83) How many bytes will be allotted for the declaration int num[4] [3]
A. 6 bytes
B. 24 bytes
C. 12 bytes
D. 48 bytes

84) If statement is a —————statement


A. loop construct
B. two way decision
C. multiway decision
D. one-way decision

85) The total memory required for an array


A. sizeof (datatype) * 2
B. size of (datatype) * size of used array elements
C. sizeof (datatype) * sizeof array
D. size of (array) * datatype

86) int arr[3][3] ={{0}, {0}} in this statement

A. none of the element are initialized to zero


B. only first row elements are initialized to zero
C. only last row elements are initialized to zero
D. all the array elements are initialized to zero

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

88) consider the array definition


int num [10] = { 3 ,3 ,3 };
pick the correct answers

A. This is syntactically wrong


B. the value of num[8] is 3
C. this is invalid if it comes within a function
D. the value of num[2] is 3
89) Give the output of the following program:
#include < stdio.h >
main()
{
int I=1;
while (I < 5)
{
printf(“%d”, I);
}
}
A. Print the value of I as 1
B. Warning for no return type for main ( )
C. Infinite loop
D. Prints the value of I as11111

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. Error message from the compiler


B. Possible system malfunction
C. Last element data may be overwritten
D. Nothing
92) Which of the following statements would read a single character from the keyboard and place
the result in a character variable ‘ch’ defined as: char ch;
A. ch = getch( );
B. printf( “%c”, ch );
C. while(!kbhit)
D. getkeyb ( ch );
93) What is the output of the following module
sum=0; I=0;
do{ sum+=I;
I++;
}while(I<=5);
printf(“%d”, sum);

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

96) Output of the below program


#include<stdio.h>
main()
{int a,b=0;
int c[10]={1,2,3,4,5,6,7,8,9,0 };
for(a=0;a<10;++a)
b+=c[a];
printf(“%d”,b);
}
A. 1 3 6 10 15 21 28 32 45 45
B. 55
C. 45
D. 0
97) Dynamic memory allocation in array results in

A. allocation of memory at debugging time


B. allocation of memory at file saving time
C. allocation of memory at compile time
D. allocation of memory at runtime
98)
main()
{
char name[5];
scanf(“%s”,name);
printf(“%s”, name);
}
if Program is the given as input, what will be the o/p of the program;
A. Progr
B. Prog
C. program
D. Runtime error
99) Which of the following is not a key word of C?
A. main
B. void
C. const
D. sizeof

100) What symbol is used to represent input/output operations in a flow chart.


A. Rectangles
B. Parellograms
C. circles
D. Rectangle with rounded end

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

107) What type of errors are checked during compilation


A. divide by zero error
B. logical errors
C. run - time errors
D. syntax errors

108) What could be output of the following function


main( )
{
int x=2,y=6,z=6;
x=y=z;
printf(“%d”,x);
}
A. 2
B. 1
C. 0
D. 6
109) How would you declare a constant of 5 called “MYCONST”?
A. #define MYCONST 5
B. var int MYCONST=5
C. int myconst = 5;
D. constant MYCONST = 5;

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

You might also like