The scope of variable 'j' is the single printf that follows it. So, the last statement that involves 'j' will complain about the undeclared identifier 'j'
Step 1: char str1[] = "Hello"; The variable str1 is declared as an array of characters and initialized with a string "Hello".
Step 2: char str2[] = "Hello"; The variable str2 is declared as an array of characters and initialized with a string "Hello". We have use strcmp(s1,s2) function to compare strings.
Step 3: if(str1 == str2) here the address of str1 and str2 are compared. The address of both variable is not same. Hence the if condition is failed.
Step 4: At the else part it prints "Unequal".
Q. No. :
3
Question :
The following program
main()
{ int abc();
abc();
(*abc)();
}
int abc()
{ printf("come"); }
Step 1: int fun(int); Here we declare the prototype of the function fun().
Step 2: int i = fun(10); The variable i is declared as an integer type and the result of the fun(10) will be stored in the variable i.
Step 3: int fun(int i){ return (i++); } Inside the fun() we are returning a value return(i++). It returns 10. because i++ is the post-increment operator.
Step 4: Then the control back to the main function and the value 10 is assigned to variable i.
Step 5: printf("%d
", --i); Here --i denoted pre-increment. Hence it prints the value 9.
Q. No. :
5
Question :
The following code fragment
int x,y = 2,z,a;
x = (y *= 2) + (z = a = y);
printf("%d",x);
A :
prints 8
B :
prints 6
C :
prints 6 or 8 d3epending on the compiler implementation
Step 1: int no=5; The variable no is declared as integer type and initialized to 5.
Step 2: reverse(no); becomes reverse(5); It calls the function reverse() with '5' as parameter.
The function reverse accept an integer number 5 and it returns '0'(zero) if(5 == 0) if the given number is '0'(zero) or else printf("%d,", no); it prints that number 5 and calls the function reverse(5);. The function runs infinetely because the there is a post-increment operator is used. So, it calls reverse(5) infinitely. loops
Q. No. :
7
Question :
What will be the output of the program ?
#include< stdio.h >
int main()
{
int a=250;
printf("%1d
", a);
return 0;
}
int a=250; The variable a is declared as an integer type and initialized to value 250. printf("%1d
", a); It prints the value of variable a. Hence the output of the program is 250.
Q. No. :
8
Question :
Which of the following statements are correct about the program below?
Step 1: void fun(); This is the prototype for the function fun().
Step 2: fun(); The function fun() is called here.
The function fun() gets a character input and the input is terminated by an enter key(New line character). It prints the given character in the reverse order.
The given input characters are "abc" Output: cba
Q. No. :
12
Question :
Consider the following statement:
#define hypoteneuse(a,b) sqrt(a*a + b*b);
The macro-call hypotenuse(a + 2,b + 3);
A :
finds the hypotenuse of a triangle with sides a + 2 and b + 2.
The program will generate infinite loop. When an EOF is encountered fgetc() returns EOF. Instead of checking the condition for EOF we have checked it for NULL. so the program will generate infinite loop.
The sizeof function return the given variable. Example: float a=10; sizeof(a) is 4 bytes
Step 1: float arr[] = {12.4, 2.3, 4.5, 6.7}; The variable arr is declared as an floating point array and it is initialized with the values. Step 2: printf("%d
", sizeof(arr)/sizeof(arr[0])); The variable arr has 4 elements. The size of the float variable is 4 bytes. Hence 4 elements x 4 bytes = 16 bytes sizeof(arr[0]) is 4 bytes Hence 16/4 is 4 bytes Hence the output of the program is '4'.
#include< stdio.h >
int check (int, int);
int main()
{
int c;
c = check(10, 20);
printf("c=%d
", c);
return 0;
}
int check(int i, int j)
{
int *p, *q;
p=&i;
q=&j;
i>=45 ? return(*p): return(*q);
}
Step 1: void swap(char *, char *); This prototype tells the compiler that the function swap accept two strings as arguments and it does not return anything.
Step 2: char *pstr[2] = {"Hello", "GPLgtests"}; The variable pstr is declared as an pointer to the array of strings. It is initialized to pstr[0] = "Hello", pstr[1] = "GPLgtests"
Step 3: swap(pstr[0], pstr[1]); The swap function is called by "call by value". Hence it does not affect the output of the program.
If the swap function is "called by reference" it will affect the variable pstr.
Step 4: printf("%s
%s", pstr[0], pstr[1]); It prints the value of pstr[0] and pstr[1].
Hence the output of the program is : Hello GPLgtests
Q. No. :
24
Question :
If the size of pointer is 4 bytes then What will be the output of the program ?
If the index of the array size is exceeded, the program will crash. Hence "option c" is the correct answer.
Example: Run the below program, it will crash in Windows (TurboC Compiler)
#include< stdio.h >
int main()
{
int arr[2];
arr[3]=10;
printf("%d",arr[3]);
return 0;
}
Since C is a compiler dependent language, it may give different outputs at different platforms. We have given the Turbo-C Compiler (Windows) output.
Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you will understand the difference better.
Q. No. :
30
Question :
Macro FILE is defined in
A :
stdio.h
B :
conio.h
C :
ctype.h
D :
string.h
Answer: A
Q. No. :
31
Question :
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 :
'->'
Answer: D
Q. No. :
32
Question :
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
", a);
return 0;
}
int a=20;
A :
extern int a is declaration, int a = 20 is the definition
B :
int a = 20 is declaration, extern int a is the definition
Step 1: x=y=z=1; here the variables x ,y, z are initialized to value '1'.
Step 2: z = ++x || ++y && ++z; becomes z = ( (++x) || (++y && ++z) ). Here ++x becomes 2. So there is no need to check the other side because ||(Logical OR) condition is satisfied.(z = (2 || ++y && ++z)). There is no need to process ++y && ++z. Hence it returns '1'. So the value of variable z is '1'
Step 3: printf("x=%d, y=%d, z=%d
", x, y, z); It prints "x=2, y=1, z=1". here x is increemented in previous step. y and z are not increemented.
Q. No. :
39
Question :
What will be the output of the program ?
#include< stdio.h >
int main()
{
int i=32, j=0x20, k, l, m;
k=i|j;
l=i&j;
m=k^l;
printf("%d, %d, %d, %d, %d
", i, j, k, l, m);
return 0;
}
The logical not operator takes expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it reverses the value of the expression.
Step 1: if(!(!x) && x)
Step 2: if(!(!10) && 10)
Step 3: if(!(0) && 10)
Step 3: if(1 && 10)
Step 4: if(TRUE) here the if condition is satisfied. Hence it prints x = 10.
if(a < 0.7) here a is a float variable and 0.7 is a double constant. The float variable a is less than double constant 0.7. Hence the if condition is satisfied and it prints 'C'
Example:
#include< stdio.h >
int main()
{
float a=0.7;
printf("%.10f %.10f
",0.7, a);
return 0;
}
Output:
0.7000000000 0.6999999881
Q. No. :
43
Question :
What does the following declaration mean?
int (*ptr)[10]
The statement 'B' is correct. ptr is a pointer to an array of 10 integers
Q. No. :
44
Question :
Which of the following statements are correct about the below C-program?
#include< stdio.h >
int main()
{
int x = 10, y = 100%90, i;
for(i=1; i<10; i++)
if(x != y);
printf("x = %d y = %d
", x, y);
return 0;
}
1 : The printf() function is called 10 times.
2 : The program will produce the output x = 10 y = 10
3 : The ; after the if(x!=y) will NOT produce an error.
4 : The program will not produce output.