C Programming
C Programming
#include<stdio.h>
int main()
{
enum checkpoint {point1, point2, point3} s1, s2;
s1 = point1;
s2 = s1 + 1;
printf("%d",s2);
return 0;
}
2
Compiler error
0
1
----------------------------------------Which one of the following is an illegal operation on a structure?
Copying & assignment
Using address operator
Direct comparison
None of these
-----------------------------------------Question Number 3
What is the correct value returned to the operating system upon the successful c
ompletion of a program?
1
0
-1
Programs do not return a value
--------------------------------------------What will be the output of the following program?
#include<stdio.h>
int main( )
{
union a
{
int i ;
char ch[2] ;
} ;
union a u ;
u.ch[0] = 3 ;
u.ch[1] = 2 ;
printf ( "%d %d %d", u.ch[0], u.ch[1], u.i ) ;
return 0;
}
3 2 515
515 2 3
3 2 5
None of these
--------------------------------------What will be the output of the following program?
#include<stdio.h>
int main()
{
int x;
x=10,20,30;
printf("%d",x);
return 0;
}
30
Compiler error
10
None of these
---------------------------------What will be the output of the following program?
#include <stdio.h>
int main()
{
int a;
printf("%d",a^a);
return 0;
}
1
0
Unexpected
Runtime error
---------------------------------------------What will be the output of the following program?
#include<stdio.h>
int main(){
int a=5,b=4;
while(a!=0)
{
if(a=((a^b)|a) == 0)
continue;
else break;
}
printf("%d", a);
return 0;
}
Infinite loop
The value 0 is printed continuously (infinite loop)
0
Runtime error
----------------------------------------------------Which one of the following statements is correct about the following function?
fun ( int a, int b )
{
int a ;
a = 20 ;
return a ;
}
Error : Redeclaration of a
None of these
-----------------------------------What will be the output of the following program?
#include<stdio.h>
int main()
{
while(1)
{
int n;
char d;
n = scanf("%c",&d);
if(d=='z')
printf("%d",n);
break;
}
return 0;
}
Infinite loop
Runtime error
Compiler error
Compiles successfully, but no output
-----------------------------------------------What will be the output of the following program?
#include<stdio.h>
int main()
{
int a = 1, b = 2, c = 3;
printf("The value of a is %d", ++a);
func();
printf(" The value of a is %d", ++a);
return 0;
}
void func()
{
int a = 10;
return 0;
}
Not equal
Equal
Compiler error
None of these
------------------------------What will be the output of the following program?
#include<stdio.h>
int main();
const int k=100;
int main()
{
int a[100];
int sum=0;
for(k=0;k<100;k++)
*(a+k)=k;
sum+=a[--k];
printf("%d",sum);
return 0;
}
0 1 2 3
It prints the address of the array elements
It is not possible to create array of references
Compiler error
---------------------------------------------The function unlink(filename) is used to __________.
remove link between file and buffer
remove the existing file from the directory
remove the contents of file only
None of these
--------------------------------------------------------------What will be the output of the following program?
#include<stdio.h>
int print_arr(float **p)
{
printf(" 0 %f 1 %f 2 %f\n",p[0][0],p[0][1],p[0][2]);
}
int main()
{
float arr[2][3] = {{0,1,2},{3,4,5}};
float **fl_arr;
fl_arr = (float *)arr;
print_arr(fl_arr);
fl_arr++;
print_arr(fl_arr);
return 0;
}
Compiler error
Runtime error
Compiles successfully
No output
-----------------------------------------------------------Which is the correct method of opening a file for writing as binary file?
FILE *f = fwrite("test.bin", "b");
FILE *f = fwriteb("test.bin");
FILE *f = fopenb("test.bin", "w");
FILE *f = fopen("test.bin", "wb");
------------------------------------------------------What will be the output of the following program?
#include<stdio.h>
int main(int argc, char *argv[])
{
char arr1[] = "Welcome";
char arr2[] = "Welcome";
if (arr1==arr2)
printf ("Both arrays are same");
else
printf ("Both arrays are not same");
return 0;
}
A.
char str[100];
strcpy(str,"Hello");
File *fp = fopen("Binary.txt", "wb");
fwrite(str,sizeof(char),strlen(str),fp);
fclose(fp);
B.
char str[100];
strcpy(str,"Hello");
File *fp = fopen("Ascii.txt", "w");
fwrite(str,sizeof(char),strlen(str),fp);
fclose(fp);
Compiler error
It will print 1 to 100
It will throw runtime Exception
None of these
------------------------Question Number 2
In the following code, what is the correct way to increment the variable ptr ?
void main()
{
int *ptr;
struct test_struct
{
int a;
float b;
};
test_struct test_array[5];
ptr = test_array;
}
Compiler error
It will open the contents of Cprogram.tmp
It will print: Cannot open file
Runtime error
--------------If the value of a = 2 and z = 6, which one of the options represents the output
value C returned by the following statement?
C = (a *= 7) + (b = z = 8)/2 ;
18
Syntax error
17
Assignment error
----------------------What will be the output of the following program?
#include<stdio.h>
int main( )
{
int k = 10, j = k++ - 20 ;
printf ( "%d %d %d", j++, j++, j++, j++ ) ;
return 0;
}
9 -10
7 -8
Compiler error
50
It prints the address
Runtime error
------------Question Number 2
What will be the output of the following program?
#include<stdio.h>
void func(){
static int i=5;
char* a="gaurav";
if(i==0)
break;
printf("%c",a[i--]);
func();
}
int main()
{
func();
return 0;
}
int main()
{
int i = 8;
int k = 2;
switch(k){
case 1<<2:
printf("%d",i);
case 8>>2:
printf("%d",i);
default:
printf("%s"," Body of lies");
}
return 0;
}
Body of lies
8 8 Body of lies
Compiler error
8 Body of lies
----------------------A function can return __________
only one value
more then one, but finite values
any number of values
None of these
--------------------Question Number 5
What will be the output of the following program ?
#include<stdio.h>
int func1 (int a, int b)
{
return (a + b);
}
float func1(float a, float b)
{
return (a/b);
}
int func1(int a,int b,int c)
{
return (a - b - c);
}
int main()
{
int i = 10;
int j = 5;
printf("%d",func1(i,j,1));
return 0;
}
15
4
3
Compiler error
-----------------------Consider test1, test2, test3 as three functions. In which order will the functio
ns be called in the following expression?
a = test1() * test2() / test3() + test3;
test1, test2, test3
test2, test3, test1
test3, test2, test1
Compiler dependent
--------------------------------What will be the output of the following program?
#include<stdio.h>
int main()
{
int i=8,j=8;
while(i+1?--i:j++)
printf("%d ",i);
return 0;
}
1
8
1 2 3 4 5 6 7
7 6 5 4 3 2 1
return 0;
}
No value
The value of the 5th element of the array pointed by the pointer ptr
The sum of 5 elements of the array pointed by the pointer ptr
The address of the 5th element of the array pointed by the pointer ptr
-------------------------------------hat will be the output of the following program?
#include<stdio.h>
int main(){
int aVar[]={9,4,1,7,5};
int *pVar;
pVar=&aVar[3];
printf("%d",pVar[-1]);
return 0;
}
6
7
1
Compiler error
---------------What will be the output of the following program?
#include<stdio.h>
int main()
{
char *buffer = "Time Travel";
char *ptr = buffer;
ptr += 5;
printf( "%s\n", ptr );
printf( "%s\n", buffer );
return 0;
}
Travel
Time Travel
Time Travel
Travel
5ime Travel
5ime Travel
Travel
Travel
------------------What will be the output of the following program ?
#include<stdio.h>
int main()
{
char *str1="abcd";
char str2[]="abcd";
printf("%d %d", sizeof(str1), sizeof(str2));
return 0;
}
4 5
1 5
1 4
1 1
---------------------uestion Number 6
What will be the output of the following program ?
#include<stdio.h>
int main()
{
char *s = "ABCDEF";
char a[6]= {'A','B','C','D','E','F'};
printf("%d", strcmp(s,a));
return 0;
}
0
Compiler error
1
-1
------------------------What will be the output of the following program ?
#include<stdio.h>
int main()
{
int a=387;
char *ptr;
ptr=(char *)&a;
printf("%d ",*ptr);
return 0;
}
3
64
-125
Compiler error
------------------------What will be the output of the program below,if MyFile.txt contains abcdefghij?
#include <stdio.h>
int main()
{
FILE* fp = NULL;
unsigned char c;
fp = fopen("MyFile.txt","r");
while((c = getc(fp)) != EOF)
putchar(c);
fclose(fp);
return 0;
Stack overflow
Infinite loop
abcdefghij followed by infinite loop
Runtime error
----------------------------Which one of the following statements is correct?
strcmp ( s1 , s2 ) returns a number greater then 0 if s1 < s2
strcmp ( s1 , s2 ) returns 0 if s1 == s2
strcmp ( s1 , s2 ) returns a number less then 0 if s1 > s2
strcmp ( s1 , s2 ) returns 1 if s1 == s2
----------------------------What will be the output of the following program?
#include <stdio.h>
int main()
{
putchar(65);
putchar(10);
putchar(66);
putchar(10);
putchar(67);
putchar(10);
return 0;
}
65 10 66 10 67 10
Compiler error
A
B
C
ABCDEF
------------------------------Question Number 11
Runtime error
2 1
2 garbage
Compiler error
-----------------------------------Which is the correct method of opening a file for writing as binary file?
FILE *f = fwrite("test.bin", "b");
FILE *f = fopenb("test.bin", "w");
FILE *f = fwriteb("test.bin");
FILE *f = fopen("test.bin", "wb");
-------------------------------------Which is the correct method of opening a file for writing as binary file?
FILE *f = fwrite("test.bin", "b");
FILE *f = fopenb("test.bin", "w");
FILE *f = fwriteb("test.bin");
FILE *f = fopen("test.bin", "wb");
-----------------------------------------What is the purpose of r+, in function fopen( ) used in the following program?
#include<stdio.h>
int main( )
{
FILE *fp ;
fp = fopen ( "NOTES.TXT", "r+" ) ;
return 0;
}
It reads the existing contents and then overwrites new contents thus modifying c
ontents of the file
It overwrites contents of an existing file
It reads existing contents and then appends the new contents to the end of file
but doesn t modify the file
It reads existing contents of a file
-------------------------------------------What will be the output of the following program?
#include<stdio.h>
int main()
{
char *p1;
char *p2;
p1=(char *) malloc(25);
p2=(char *) malloc(25);
strcpy(p1,"String");
strcpy(p2,"Array");
strcat(p1,p2);
printf("%s",p1);
return 0;
}
NULL
StringArray
Compiler error
None of these
--------------------------------------EXPERT LEVEL
What will be the output of the following program?
#include<stdio.h>
enum day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};
int main()
{
enum day today;
for(today=SUNDAY; today<=SATURDAY; today++)
printf("%d ",today);
return 0;
}
Compiler error
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
0
0 1 2 3 4 5 6
------------------------------------What will be the output of the following program?
#include<stdio.h>
union union_tag{
union in_union{
int i;
char * nickname;
}b;
int a;
char * name;
};
int main()
{
union union_tag x = {12};
printf("%d",x.b.i);
return 0;
}
Compiler error
12
Garbage
Nesting of union leads to unpredictable results
-------------------------What will be the output of the following program?
#include<stdio.h>
#define sdef struct s *
struct s{int x;char y[2];};
int main()
{
sdef sobj1,sobj2;
sobj1->x=10;sobj1->y[0]='a';
sobj1->y[0]='b';sobj2=sobj1;
printf("%d%c%c",sobj2->x, sobj2->y[0],sobj2->y[1]);
return 0;
}
Runtime error
Compiler warning
Compiler error
10ab
------------------------------Which one of the following is not a user-defined data type?
struct book
{
char name[10] ;
float price ;
int pages ;
};
Bob
Garbage value
26
Compiler error
-----------------------------What will be the output of the following program?
struct Adv{
int p;
float q;
long double *r;
};
void main(){
struct Adv aList[10];
clrscr();
printf("%d",sizeof aList);
getch();
}
10
Compiler dependent
80
100
--------------------------------What will be the output of the following program?
#include<stdio.h>
int main ( )
{
int i = 3 , j=0 ,ii;
j = add ( ++i) ;
printf ( "i =%d j = %d",i, j ) ;
return 0;
}
add ( int ii )
{
ii++ ;
printf ( "ii = %d \n" , ii ) ;
return 0;
}
Compiler error
ii = 5
i =4 j = 0
ii = 3
i =3 j = 0
ii = 5
i =3 j = 0
---------------------------------What will the following program do?
binary(int n)
{
if(n==0)
return;
else{
binary(n/2);
printf("%d",(n%2));
}
}
5 4 3 2 1 0
5 4 3 2 1
4 3 2 1 0
Compiler error
---------------------------------What will be the output of the following program?
#include<stdio.h>
int main()
{
int i=255, j=255;
i>>2;
i<<2;
if(i<j)
printf("Condition 1");
else
printf("Condition 1");
return 0;
}
Condition 1
Condition 2
Compiler error
None of these
---------------------------What will be the output of the following program?
#include<stdio.h>
void fun(char *a)
{
char * b = "C unleashed";
b = a;
}
int main()
{
char* a;
fun(a);
printf("%d",*a);
return 0;
}
Compiler error
-----------------------------What will be the output of the following program?
#include<stdio.h>
int main()
{
int i=1;
if(!i)
printf("It is up\n");
else
{
i=0;
printf("It is down\n");
main();
}
return 0;
}
It is down
It is up
It is up
It is down
It is down till stack overflow
None of these
-------------------------What will be the output of the following program?
void main()
{
int i=1;
switch(i)
{
lab:
case 0:
printf("%d", i);
break;
case 1:
++i;
printf("%d", i);
goto lab;
}
}
2 1
Compiler error
2 2
1 2
-------------------------What will be the output of the following program?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
func(int *a);
int main()
{
const int *i;
int a = 10;
i = &a;
func(i);
printf("%d",*i);
getch();
}
func(int *a)
{
a++;
*a = 100;
}
100
Error in line 07
Error in line 07 & 13
10
-----------------------------What will be the output of the following program?
#include<stdio.h>
char* func()
{
char str[10] = "Take Care";
return str;
}
int main()
{
char* str1 = func();
printf("%s", str1);
return 0;
}
Take Care
Compiler error
Undefined value
None of these
-------------------What will be the output of the following program?
#include<stdio.h>
int main(){
char b,c;
for(printf("a\n");
printf("%c\n",b<c?'b':'c');
printf("c\n"));
printf("\n");
return 0;
}
bca
Compiler error
Infinite loop of bca
Infinite loop of c
-----------------------Point out the error if any, in the following code snippet?
#include <stdio.h>
int main()
{
int day,year;
char month[12];
clrscr();
scanf("%d %s %d",&day,month,&year);
printf("%d %s %d",day,month,year);
getch();
return 0;
}
Consider the input as: 23
Jan
83
#include <stdio.h>
char *someFun()
{
char *temp = "local string";
return temp;
}
int main()
{
puts(someFun());
return 0;
}
Runtime error
Compiler error
local string
Undefined
----------------------------Consider the following two-dimensional array:
int twoarray[4][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Select the correct option that gives out the values of the following array eleme
nts.
twoarray[1][1], twoarray[2][0], twoarray[3][2], twoarray[0][2]
5, 7, 12, 1
5, 12, 7 , 3
5, 7, 12, 3
6, 9, 10, 2
------------------------------------What will be the output of the following program?
#include<stdio.h>
#define putchar (c) printf("%c",c)
int main()
{
char s='c';
putchar (s);
return 0;
}
Compiler error
99
Runtime error
C
--------------------------------What will be the output of the following program?
#include<stdio.h>
void print(int *b)
{
int i, j;
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d ", *(b+i*2+j));
}
}
}
int main()
{
int a[2][2] = {1,2,3,4};
print(a);
return 0;
}
4 3 2 1
1 2 3 4
Compiler error
Garbage values
----------------------What will the content of Tree.dat file in the following program?
#include <stdio.h>
int main()
{
FILE *fp;
fp=fopen("Tree.dat","w");
fprintf(fp, "Root");
fclose(fp);
fp=fopen("Tree.dat","w");
fprintf(fp, "Leaf");
fclose(fp);
return 0;
}
RootLeaf
Root
Leaf
No Content
---------------------------Which one of the following statements represent a correct and safe declaration o
f a NULL pointer?
typedef((void *)0) NULL;
typedef NULL(char *)0;
#define NULL((void *)0)
None of these
---------------------------------Select the function which does not generate file names that can be safely used f
or a temporary file.
char *tmpname_r(char *s)
char *tempname r(const char *dir, const char *pfx)
char *tmpname(char *s)
char *tempname(const char *dir, const char *pfx)
-------------------------------------What will be the output of function below in ideal realtime situation?
int func(volatile int *ptr){
int x,y;x=*ptr;y=*ptr;
return x*y;}
Compilation warning
--------------------------------What will be the output of the following program?
#include <stdio.h>
#define BUFSIZE 1024
void usage(char *cmd)
{
printf("Usage: %s <file to read with complete path>\n", cmd);
printf("If your file is at /home/Cprogramming/documents/names.txt,
then the command will be: \n");
printf("%s /home/ Cprogramming /documents/names.txt\n", cmd);
}
int main(int argc, char *argv[])
{
FILE *fp;
if (argc < 2)
{
usage(argv[0]);
exit(0);
}
fp = fopen(argv[1], "r");
if (fp == NULL)
{
usage(argv[0]);
exit(0);
}
printf("Contents of %s are: \n", argv[1]);
while (!feof(fp))
{
char buf[BUFSIZE] = {0};
fread(buf, sizeof(char), BUFSIZE, fp);
printf("%s", buf);
}
printf("End of the file\n");
fclose(fp);
return 0;
}
Compiler error
Runtime error
No output
None of these
-----------------------------Consider the following program:
#include <alloc.h>
int main( )
{
int **p , i ;
p = ( int ** ) malloc ( sizeof ( int * ) * 3 ) ;
for ( i = 0 ; I < 3 ; i++ )
p[ i ] = (int * ) malloc ( 4*sizeof ( int ) ) ;
return 0;
}
How would you free the memory allocated by above program?
for ( i = 0 ; i < 3; i ++) free ( p[i] ) ;
for ( i = 0 ; i < 4 ; i++ ) free ( p[i] ) ; free ( p ) ;
for( i = 0 ; i < 3 ;i++ ) free ( p[i] ) ; free ( p ) ;
free ( **p ) ;
-------------------------------------------What will be the output of the following program?
#include<stdio.h>
open(char *a,FILE **ptr){
*ptr = fopen(a,"r");
}
int main()
{
char *c;
file *ptr;
open("C:\\test.txt",*ptr);
while (fgets(c,100,ptr)!=NULL){
puts(c);
}
fclose(ptr);
getch();
return 0;
}
It will print the last line of the file twice while printing the contents of the
file
It will run into an infinite loop
Compiler error
It will print the contents of the file till it encounters the EOF
10.450000 A
10.45
Garbage value A
10 65
------------------------------------What will be the output of the following program?
#include<stdio.h>
int f(){
static int i;
i--;
return i;
}
int main()
{
int a = 10;
a=f();
printf("%d",a);
return 0;
}
-1
1
10
Garbage value
------------------------------What will be the output of the following program?
#include<stdio.h>
int main()
{
int arr[] = {1,2,3,4,5,6};
static int i;
for(;i<3;i++)
printf("%d ",arr[arr[i]]++);
return 0;
}
2 4 5
3 5
Runtime error
Compiler error
--------------------------------What will be the output of the following program?
typedef struct { int a;short b; } sFile;
int main()
{
sFile sData ={0,0};
int d=1000;
short i=20;
FILE* fp=fopen("MyFile.dat", "wb");
if(1!=fwrite(&d,sizeof(d),1,fp)) return 1;
if(1!= fwrite(&i,sizeof(i),1,fp)) return 1;
fclose(fp);
fp = fopen("MyFile.dat","rb");
if(fread(&sData,sizeof(sData),1,fp))!= 1)
return 1;
fprintf(stdout,"%d",sData.a);
fclose(fp);
return 0;
}
* ( * ( * ( a + i ) + j ) + k ) + l
* ( * ( * ( * ( a + i ) j ) k ) l )
* ( * ( * ( * ( a + i ) + j ) + k ) + l )
( a + i ) + j ) + k ) + l
-----------------------------What will be the output of the following program?
Assume numbers2.txt contains the following data
111 222 333
444 555 666
777 888 999
int main()
{
char c[10];
FILE *file;
file = fopen("numbers2.txt", "r");
if(file==NULL)
{
printf("Error: can't open file.\n");
return 1;
}
else
{
printf("File opened successfully. Contents:\n\n");
while(fgets(c, 10, file)!=NULL)
{
printf("String: %s", c);
}
printf("\n\nNow closing file...\n");
fclose(file);
return 0;
}
}
4
4*size(int)
-3
0
-------------------------------------What will be the output of the following program?
#include<stdio.h>
#include<stdlib.h>
int main( )
{
unsigned char ch ;
FILE *fp ;
fp = fopen ( "trial.c", "r" ) ;
if ( !fp )
{
printf ( "Unable to open the file" ) ;
exit ( 0 ) ;
}
fclose ( fp ) ;
return 0;
}
0
Garbage
20
None of these
-------------------------------