1) What will be the output/error?
#include
union test
{
int x, y;
};
int main()
{
union test t;
t.x = 2;
printf ("%d, %d\n",t.x, t.y);
t.y = 10;
printf ("%d, %d\n",t.x, t.y);
return 0;
}
Options:
a) 2, 0
2, 10
b) 2, 0
0, 10
c) 2, 2
10, 10
d) Compilation error
Size of a union is taken according to the size of largest member in union. In union, all members share the same memory location. In this program, both x and y share the same location. If we change x, we can see the changes being reflected in y and vice versa.
2) What will be the output/error?
#include
int var = 20;
int main()
{
int var = var;
printf("%d ", var);
return 0;
}
First var is declared, then value is assigned to it. As soon as var is declared as a local variable, it hides the global variable var.
Options:
a)Garbage Value
b)20
c)Compile time error
d)Run time error
3) What will be the output of the program ?
#include
int main()
{
if(!printf("Welcome "));
else
printf("C\n");
return 0;
}
in if(!printf("Welcome ")) statement printf will print "Welcome " and return 8 (number of printed character), hence condition will be false(!6 that is 0), then execution will jump to else block.
a) Error
b) Welcome
c) C
d) Welcome C
4) What will be the output of the program ?
#include
int main()
{
FILE *fp;
char c;
fp = fopen(__FILE__, "r");
do
{
c=fgetc(fp);
putchar(c);
}
while(c!=EOF);
fclose(fp);
return 0;
}
you can print your current source code file, use __FILE__ to specify current file name and read the file (using file handling) and print it on screen, that will your output.
a) 0
b) null
c) Print the source code as output
d) Error
5) Predict the output:
main()
{
int i=10;
i=!i>14;
printf("i=%d",i);
}
In the expression !i>14 , NOT (!) operator has more precedence than ‘ >’ symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).
a) true
b) Compilation error
c) false
d) 0
6) Predict the output:
#include
main()
{
int a[2][2][2] = { {1,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
}
p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the third 2D (which you are not declared) it will print garbage values. *q=***a starting address of a is assigned integer pointer. Now q is pointing to starting address of a. If you print *q, it will print first element of 3D array.
a) Some Garbage Value---1
b) Compilation error
c) Some Garbage Value---2
d) 0
7) Predict the output:
#include
main()
{
struct xx
{
int x=3;
char name[]="hello";
};
struct xx *s;
printf("%d",s->x);
printf("%s",s->name);
}
You should not initialize variables in declaration
a) Some Garbage Value
b) Compilation error
c) hello
d) 0
8) Predict the output:
#include
main()
{
struct xx
{
int x;
struct yy
{
char s;
struct xx *p;
};
struct yy *q;
};
}
The structure yy is nested within structure xx. Hence, the elements are of yy are to be accessed through the instance of structure xx, which needs an instance of yy to be known. If the instance is created after defining the structure the compiler will not know about the instance relative to xx. Hence for nested structure yy you have to declare a structure variable.
#include
int main()
{
struct xx
{
int x;
struct yy
{
char s;
struct xx *p;
}y1;
struct yy *q;
};
}a) No output
b) Runtime Error
c) Compilation error
d) 0
9) Predict the output:
main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}
\n - newline
\b - backspace
\r – linefeed
Use https://www.onlinegdb.com/online_c_compiler to run this code. Other online compilers might not give the actual output. a) ab
b) absi
c) Compilation error
d) hai
10) Predict the output:
#include
int main()
{
char a[]="hai friends",*p1,*p;
p=a;
p1=p;
while(*p!='\0')
++*p++;
printf("%s%s",p,pa);
}
++*p++ will be parse in the given order
*p that is value at the location currently pointed by p will be taken
++*p the retrieved value will be incremented
when ; is encountered the location will be incremented that is p++ will be executed
Hence, in the while loop initial value pointed by p is ‘h’, which is changed to ‘i’ by executing ++*p
and pointer moves to point, ‘a’ which is similarly changed to ‘b’ and so on. Similarly blank space is
converted to ‘!’. Thus, we obtain value in p becomes “ibj!gsjfoet” and since p reaches ‘\0’ and p1
points to p thus p doesnot print anything.
a) friends
b) ibj!gsjfoet
c) Compilation error
d) Ib!sjoet
11) Predict the output:
#define clrscr() 100
main()
{
clrscr();
printf("%d\n",clrscr());
}
Preprocessor will replace clrscr() with 100.
a) clrscr()
b) 1
c) Compilation error
d) 100
12) Predict the output:
main()
{
char *p;
p="Hello";
printf("%c\n",*&*p);
}
* is a dereference operator & is a reference operator. They can be applied any
number of times provided it is meaningful. Here p points to the first character in the
string "Hello". *p dereferences it and so its value is H. Again & references it to an
address and * dereferences it to the value H.
a) H
b) e
c) Hello
d) Compilation error
13) Predict the output:
main()
{
static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
int i;
char *t;
t=names[3];
names[3]=names[4];
names[4]=t;
for (i=0;i<=4;i++)
printf("%s",names[i]);
}
assignment to expression with array type. The compilation error will be removed If we use strcpy instead of names[3]=names[4]; & names[4]=t;
i.e strcpy(names[3],names[4]); strcpy(names[4],t);
a) Pascal
b) ada
c) Pascal, ada, cobol, fortran, perl
d) Compilation error
14) Predict the output:
void main()
{
int i=5;
printf("%d",i+++++i);
}
The expression i+++++i is parsed as i ++ ++ + i which is an illegal combination of operators. We should write printf("%d",i++ + ++i);
a) Runtime error
b) Compilation error
c) 10
d) 55
15) Predict the output:
main()
{
int i;
printf("%d",scanf("%d”, i)); // value 10 is given as input here
}
Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1.
a) Runtime error
b) Compilation error
c) 10
d) 1
16) Predict the output:
#define f(g,gb) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
}
## is the preprocessor "command" for concatenating what comes before and after.
a) 00100
b) Compilation error
c) 100
d) 1
17) Predict the output:
#include
main()
{
char s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%c",++*p + ++*str1-32);
}
p is pointing to character '\n'.str1 is pointing to character 'a' ++*p me Answer:"p is
pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10. then it is
incremented to 11. the value of ++*p is 11. ++*str1 me Answer:"str1 is pointing to 'a'
that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. both 11 and 98 is
added and result is subtracted from 32.
i.e. (11+98-32)=77("M");
a) N
b)M
O
L
18) Predict the output:
main( )
{
void *vp;
char ch = ‘g’, *cp = “goofy”;
int j = 20;
vp = &ch;
printf(“%c”, *(char *)vp);
vp = &j;
printf(“%d”,*(int *)vp);
vp = cp;
printf(“%s”,(char *)vp + c);
}
Since a void pointer is used it can be type casted to any other type pointer. vp = &ch
stores address of char ch and the next statement prints the value stored in vp after type
casting it to the proper data type pointer. the output is ‘g’. Similarly the output from
second printf is ‘20’. The third printf statement type casts it to print the string from the
4th value hence the output is ‘fy’.
a) g20
b) g20f
c) g20fy
d) Compilation error
19) In a C program, _____________ are processed by preprocessor which is a special program invoked by the compiler.
a) Header files
b) Macros
c) All lines that start with #
d) None
In a C program, all lines that start with # (Header files as well as macros) are processed by preprocessor which is a special program invoked by the compiler. In a very basic term, preprocessor takes a C program and produces another C program without any #.
20) Which of the following is not a storage class specifier in C?
a) static
b) volatile
c) Register
volatile is not a storage class specifier. volatile and const are type qualifiers.
21) Comment on below statement (In C Language) int main(void) { }
a) Main does not return any value
b) main can only be called without any parameter
c) main can only be called with some parameter
If we did not specify any argument in main function then we can pass any number of arguments. But if we specify void, we cannot pass arguments.
22) Comment the output of below two print statements.
integer x=10,
y=20,z=5
print x*y/z+x
print x*(y/z)+x
a) Same output
b) Differ by 20
c) Differ by 10
d) Differ by 15
print x*y/z+x //*(multiplication) and / (Division) having higher precedence than +(Addition) . Both /* has same precedence, so it will be executed from left to right. 10*20=200; 200/5=40; 40+10=50
print x*(y/z)+x //() - paranthesis has highest precedence than +,*,/ 20/5=4; 4*10=40; 40+10=50
Output of both print statements are same.
23) What is the correct syntax to send a 3-dimensional array as a parameter? (Assuming declaration int a[5][4][3];)
a) func(a);
b) func(&a);
c) func(*a);
d) func(**a);
While sending 3D array as parameter, we need to simply pass its name alone.
24) Suji wants to create new datatype for date(1 to 31) and month(1 to 12). The number of bits required to store these two data-types will respectively be:
a) 5 and 4
b) 31 and 12
c) 4 and 4
d) 2 and 2
Since we know that the value of date is always from 1 to 31, value of month is from 1 to 12, we can optimize the space using bit fields. Date has value between 1 and 31, so 5(16,8,4,2,a) bits are sufficient, month has value between 1 and 12, so 4(8,4,2,a) bits are sufficient.
25) A single character needs 3 bytes of memory. There is an array of character holding the value "FOCUS ACADEMY". Starting address of that array is 1010. What will be the ending address of character "M"?
a) 1043
b) 1042
c) 1046
d) 1045
There are 11 letters(including space) before "M". As per the question each character will occupy 3 bytes. 3*11=33. 1010+33=1043. 1043 will be the starting address of "M", so ending address will be 1045.
26) Anoo wants to make a program to print first 10 multiples of 7. She writes the below code:
integer a=0,s=0
while(s<=10) //Statement-1
{
a=a+7 //Statement-2
print a //Statement-3
s=s+1; //Statement-4
}
Is there any error in this logic? If yes,
then which statement will be modified to get the correct output?
While loop need to be executed for 10 times to print 10 multiples of 7. Initial value of s is 0. So the condition might be s<10 .="" 9-10="" b="" be="" done="" in="" loops="" modification="" needs="" or="" s="" statement-1.="" the="" to="">
a) No Error
b) Statement-1
c) Statement-2
d) Statement-310>
27) What will be the output of the program ?
#include
int main()
{
int plus(int),m=1;
for(; plus(m); m++)
printf("%3d", m);
return 0;
}
int plus (int k)
{
if(k==10)
return 0;
else return(k);
}
Plus function return the value of k until k = 10
a) 1 2 3 4 5 6 7 8 9
b) 1 2 3 4
c) Compilation error
d) Runtime error
28) What will be the output of the program ?
#include
void main()
{
int i, j, k;
char first_name[]="ANANDA";
char second_name[]="MURUGAN";
char last_name[] = "SELVARAJ";
char name[30];
for(i=0; first_name[i]!='\0'; i++)
name[i]=first_name[i];
name[i]=' ';
for(j=0; second_name[j]!='\0';j++)
name[i+j+1]=second_name[j];
name[i+j+1]=' ';
for(k=0; last_name[k]!='\0';k++)
name[i+j+k+2]=last_name[k];
name[i+j+k+2]='\0';
printf("%s\n", name);
}
In this code we are concatenating first name, second name & last name.
a) ANANDA MURUGAN SELVARAJ
b) ANANDA MURUGANSELVARAJ
c) ANANDA SELVARAJ MURUGAN
d) MURUGAN ANANDA SELVARAJ
29) What will be the output of the program ?
#include
#include
void main()
{
float p, cost, p1, cost1; for(p=0; p<=10; p=p+0.a)
{
cost=40-8*p+p*p; if(p= =0)
{
cost1=cost; continue;
}
if(cost>=costa)
break;
cost1=cost;
p1=p;
}
p=(p+pa)/2.0;
cost=40-8*p+p*p;
printf(“MINIMUM COST=%.2f AT p=%.1f”, cost, p);
}
Minimum Cost Problem: The cost of operation of a unit consists of two components C1 and C2 which can be expressed as functions of a parameter p as follows: C1 = 30 - 8p C2 = 10 + p2 The parameter p ranges from 0 to 10. Determine the value of p with an accuracy of + 0.1 where the cost of operation would be minimum. Problem Analysis: Total cost = C1 + C2 = 40 - 8p + p2 The cost is 40 when p = 0, and 33 when p = 1 and 60 when p = 10. The cost, therefore, decreases first and then increases. The program in Fig.6.14 evaluates the cost at successive intervals of p (in steps of 0.a) and stops when the cost begins to increase. The program employs break and continue statements to exit the loop.
No need to concentrate on question, just process the operations and get the output.
a) Compilation error
b) Run time error
c) MINIMUM COST = 24.00 AT p=4.0
d) MINIMUM COST = 24.05 AT p=4.5
30) What will be the output of the program ?
void main()
{
int i;
char a[]="\0";
if(printf("%s\n",a))
printf("Ok here \n");
else
printf("Forget it\n");
}
Printf will return how many characters does it print. Hence printing a null character returns 1 which makes the if statement true, thus "Ok here" is printed.
a) Forget it
b) Ok here
c) Compilation error
d) Exception
31) What will be the output of the program ?
void main()
{
unsigned giveit=-1;
int gotit;
printf("%u ",++giveit);
printf("%u \n",gotit=--giveit);
}
++ is a preincrement here, It means that increment the value of giveit before printing, hence giveit become 0, and that will get printed. Now giveit = 0
Here you are assigning value of giveit to gotit, but before doing that perform decrement because of predecrement Now giveit = -1 then gotit will get -1 Value of gotit get printed here.
a) 1 0
b) 0 some garbage value
c) 0 -1
d) 0 0
32) What will be the output of the program ?
void main()
{
char a[]="12345\0";
int i=strlen(a);
printf("here in 3 %d\n",++i);
}
The char array 'a' will hold the initialized string, whose length will be counted from 0
till the null character. Hence the 'I' will hold the value equal to 5, after the pre-increment in
the printf statement, the 6 will be printed.
a) here in 3 5
b) here in 3 6
c) Error
d) Exception
33) What will be the output of the program ?
#include
void main()
{
int k=ret(sizeof(float));
printf("\n here value is %d",++k);
}
int ret(int ret)
{
ret += 2.5;
return(ret);
}
The int ret(int ret), ie., the function name and the argument name can be the same.
Firstly, the function ret() is called in which the sizeof(float) ie., 4 is passed, after the
first expression the value in ret will be 6, as ret is integer hence the value stored in ret will
have implicit type conversion from float to int. The ret is returned in main() it is printed after
and preincrement.
) Here value is 7
b) Here value is 9
Here value is 11
Here value is 7.5
34) What will be the output of the program ?
void main()
{
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}
The variable "I" is declared as static, hence memory for I will be allocated for only
once, as it encounters the statement. The function main() will be called recursively unless I
becomes equal to 0, and since main() is recursively called, so the value of static I ie., 0 will be
printed every time the control is returned.
a) 5 5 5 5
b) 0 0 0 0
c) 4 4 4 4
d) 1 1 1 1
35) What will be the output/error?
#include
#define FOO 123
#define STRING(x) #x
void main()
{
int a = FOO;
char* c = STRING(IGetQuoted);
printf("%d%s", a, c);
}
'#' is used to typecast any input to a string
a) 123IGetQuoted
b) 123 x
c) 123 0
d) Compilation error
36) What will be the output of the program ?
#include
int sample(int n);
int main()
{
int num=20;
printf("%d",sample(num));
return 0;
}
int sample(int n)
{
if(n != 0)
return n + sample(n-a);
else
return n;
}
This will print sum of natural numbers up to 20.
a) 190
b) 231
c) 210
d) Error
37) Predict the output:
#include
void aaa()
{
printf("hi");
}
void bbb()
{
printf("hello");
}
void ccc()
{
printf("bye");
}
int main()
{
int (*ptr[3])();
ptr[0]=aaa;
ptr[1]=bbb;
ptr[2]=ccc;
ptr[2]();
}
ptr is array of pointers to functions of return type int.ptr[0] is assigned to address of
the function aaa. Similarly ptr[1] and ptr[2] for bbb and ccc respectively. ptr[2]() is in
effect of writing ccc(), since ptr[2] points to ccc.
a) hi
b) hello
c) bye
d) Compilation error
38) Predict the output:
main()
{
char *p;
p="%d\n";
p++;
p++;
printf(p-2,300);
}
The pointer points to % since it is incremented twice and again decremented by 2, it points to '%d\n' and 300 is printed.
a) 300
b) 299
c) -298
d) Compilation error
39) main()
{
int a= 0;
int b = 20;
char x =1;
char y =10;
if(a,b,x,y)
printf("hello");
}
The comma operator has associativity from left to right. Only the rightmost value is
returned and the other values are evaluated and ignored. Thus the value of last
variable y is returned to check in if. Since it is a non zero value if becomes true so,
"hello" will be printed.
a) No Output
b) hello
c) Runtime error
d) Compilation error
40) Predict the output:
main()
{
int i;
i = abc();
printf("%d",i);
}
abc()
{
_AX = 1000;
}
Normally the return value from the function is through the information from the accumulator. Here _AX is the pseudoglobal variable denoting the accumulator. Hence, the value of the accumulator is set 1000 so the function returns value 1000.
a) Return value missing
b) Runtime error
c) Compilation error
d) 1000
41) Predict the output:
#include
int main()
{
int i, j, rows=5;
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
This code will print print half pyramid using *. Shows in option 1.
a)
*
* *
* * *
* * * *
* * * * *
b)
* * * * *
* * * *
* * *
* *
*
c)
*
* *
* * *
* * * *
* * * * *
d)
* * * * *
* * * *
* * *
* *
*
42) Predict the output: (if a= 67)
#include
#include
43) Predict the output: (if n= 65d)
#include
#include
void main()
{
int n,s=0,m;
clrscr();
scanf("%d",&n);
while(n>0)
{
m=n%10;
s=s+m;
n=n/10;
}
printf("%d",s);
getch();
}
This Program is to Compute the Sum of Digits in a given Integer.
a) 5
b) 15
c) 10
d) 8
44) Predict the output:
#include
void main()
{
int a = 10, b = 20, c;
asm
{
mov ax,a
mov bx,b
add ax,bx
mov c,ax
}
printf(" %d",c);
}
It will not work in online compiler. Assembly code will assign the value of a & b to ax & bx respectively. It will store the added value in c.
a) Exception b) 10
c) 20
d) 30
45) Predict the output:
#include
#define start main
void start()
{
printf("Hello");
}
C Program Without using Main Function. Instead we used macro.
a) Exception
b) Compilation error
c) Hello
d) 1
46) Predict the output:
#include
#include
47) Predict the output:
#include
int find(int a,int b)
{
int temp;
if(a==0 || b==0)
return 0;
while(b!=0)
{
temp = a%b;
a = b;
b = temp;
}
return a;
}
int main()
{
int a=100,b=40;
int x;
x=find(a,b);
printf(“%d\n",x);
return 0;
}
Return the remainder if any or return divisor.
a) 40
b) 20
c) 100
d) 80
48) Predict the output:
#include
int main()
{
printf("\n\"Hello\tWorld!\"");
return 0;
}
\“ will print “
\t will print tab
\n will print new line
a) \HelloWorld!\
b) "HelloWorld!”
c) "Hello World!”
d) Error
49) What is the output of this C code?
#include
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **p);
}
a) 5 5 5
b) 5 5 junk
c) 5 junk junk
d) Compile time error
50) Which of the following statements about stdout and stderr are true?
a) They both are the same
b) Run time errors are automatically displayed in stderr
c) Both are connected to the screen by default.
d) stdout is line buffered but stderr is unbuffered.
51) Given the below statements about C programming language
a) main() function should always be the first function present in a C program file
b) all the elements of an union share their memory location
c) A void pointer can hold address of any type and can be typcasted to any type
d) A static variable hold random junk value if it is not initialised
Which of the above are correct statements
a) 2,3
b) 1,2
c) 1,2,3
d) 1,2,3,4
52) If a function is defined as static, it means
a) The value returned by the function does not change
b) all the variable declared inside the function automatically will be assigned initial value of zero
c) It should be called only within the same source code / program file.
d) None of the other choices as it is wrong to add static prefix to a function
53) Comment on the below while statement
while(0 == 0) { }
a) It has syntax error as there are no statements within braces {}
b) It will run for ever
c) It compares 0 with 0 and since they are equal it will exit the loop immediately
d) It has syntax error as the same number is being compared with itself