C Programming Questions and Answers
What is the output of the program given below ?
#includeint main()
{
enum status { pass, fail, atkt};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = atkt;
stud3 = fail;
printf("%d, %d, %d\n", stud1, stud2, stud3);
return0;
}
Answer:Option C
Explanation :
enum takes the format like {0,1,2..) so pass=0, fail=1, atkt=2
stud1 = pass (value is 0)
stud2 = atkt (value is 2)
stud3 = fail (value is 1)
Hence it prints 0, 2, 1
What will be the output of the program in 16 bit platform (Turbo C under DOS)?
#includeint main()
{
externint i;
i = 20;
printf("%d\n", sizeof(i));
return0;
}
Answer:Option D
Explanation :
Linker Error : Undefined symbol 'i'
The statement extern int i specifies to the compiler that the memory for 'i' is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name 'i' is available in any other program with memory space allocated for it. Hence a linker error has occurred.
Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1 ?
Explanation :
This function is the same as the modulus operator. But fmod() performs floating point divisions.
#include #include int main ()
{
printf ("fmod of 3.14/2.1 is %lf\n", fmod (3.14,2.1) );
return0;
}
Output:fmod of 3.14/2.1 is 1.040000
What are the types of linkages?
Answer:Option C
Explanation :
External Linkage-> means global, non-static variables and functions.
Internal Linkage-> means static variables and functions with file scope.
None Linkage-> means Local variables.
Which of the following special symbol allowed in a variable name?
Answer:Option D
Explanation :
Variable names in C are made up of letters (upper and lower case) and digits. The underscore character ("_") is also permitted. Names must not begin with a digit.
Examples of valid (but not very descriptive) C variable names:
=> foo
=> Bar
=> BAZ
=> foo_bar
=> _foo42
=> _
=> QuUx
Which of the following is not user defined data type?
1 : |
|
2 : |
|
3 : |
|
Answer:Option B
Explanation :
C data types classification are
Primary data types
1. int
2.char
3. float
4. double
5.void
Secondary data types (or) User-defined data type
1. Array
2. Pointer
3. Structure
4. Union
5. Enum
So, clearly long int l = 2.35; is not User-defined data type.
(i.e.long int l = 2.35; is the answer.)
What is the output of the program?
#includeint main()
{
int x = 10, y = 20, z = 5, i;
i = x < y < z;
printf("%d\n", i);
return0;
}
Answer:Option B
Explanation :
Since x < y turns to be TRUE it is replaced by 1. Then 1 < z is compared and to be TRUE. The 1 is assigned to i.
What is the output of the program?
#includeint main()
{
externint fun(float);
int a;
a = fun(3.14);
printf("%d\n", a);
return0;
}
int fun(int aa)
{
return (int)++aa;
}
Answer:Option D
Explanation :
2 Errors
1. Type mismatch in redeclaration of fun
2. Type mismatch in parameter aa
What is the output of the program?
#include
int main()
{
int a[5] = {2, 3};
printf("%d, %d, %d\n", a[2], a[3], a[4]);
return 0;
}
Answer: Option D
Explanation :
When an automatic array is partially initialized, the remaining elements are initialized to 0.
In the following program how long will the for loop get executed?
#include
int main()
{
int i=5;
for(;scanf("%s", &i); printf("%d\n", i));
return 0;
}
Answer:Option D
Explanation :
During the for loop execution scanf() ask input and then printf() prints that given input. This process will be continued repeatedly because, scanf() returns the number of input given, the condition is always true(user gives a input means it reurns '1').
Hence this for loop would get executed infinite times.