Programming Questions
Practice Programming MCQs with answers and explanations. Page 6 of 6.
Category
Technical Questions
Topic
Programming
Page
6 / 6
Mode
Practice
Questions
Open any question to view the answer and explanation.
For the following C program, what is the most appropriate description of its behaviour?
int *m() {
int *p = 5; /* attempt to assign an integer constant to a pointer */
return p;
}
int main() {
int *k = m();
printf("%d", k);
return 0;
}
Open
View answer
In the following C program using an extern declaration, what happens at compile and link time?
int main() {
extern int i;
i = 20;
printf("%d", sizeof(i));
return 0;
}
Open
View answer
In C at file scope, what is the difference between the declarations extern int fun(); and int fun(); when no definition appears in the same translation unit?
Open
View answer
In the following C program that uses a function pointer, what is the error?
void fun() {
printf("Loud and clear");
}
int main() {
int (*p)() = fun;
(*P)();
return 0;
}
Open
View answer
In the following C program, what is wrong with the while loop?
int main() {
int i = 1;
while () {
printf("%d", i++);
if (i > 10) break;
}
return 0;
}
Open
View answer
Point out the error in the following C program that uses a switch statement and a variable in a case label:
int main() {
int i = 4, j = 2;
switch (i) {
case 1:
printf("To err is human, to forgive is against company policy.");
break;
case j:
printf("If you have nothing to do, do not do it here.");
break;
}
return 0;
}
Open
View answer
What is the output of the following C program that uses a static array and the post-increment operator?
int main() {
static int a[20];
int i = 0;
a[i] = i++;
printf("
%d%d%d", a[0], a[1], i);
return 0;
}
Open
View answer
In the following C program, what is the correct description of its behaviour?
int main() {
int i = 2;
printf("
%d%d", ++i, ++i);
return 0;
}
Open
View answer
In standard C, what can you say about the order in which the functions f1, f2, and f3 are called in the following expression?
a = ( f1(23, 14) * f2(12 / 4) ) + f3();
Open
View answer
In the following C program that reads an array of structures, which problem are you most likely to encounter at runtime?
int main() {
struct emp {
char name[20];
float sal;
};
struct emp e[10];
int i;
for (i = 0; i <= 9; i++) {
scanf("%s %f", e[i].name, &e[i].sal);
}
return 0;
}
Open
View answer
What is the output of the following C program on a typical system where sizeof(float) is 4 bytes and sizeof(double) is 8 bytes?
int main() {
printf(" %d%d%d ", (int)sizeof(3.14f), (int)sizeof(3.14), (int)sizeof(3.141));
return 0;
}
Open
View answer
What would be the output of the following C program that compares a float value initialised with 0.7 to the float literal 0.7f in an if condition?
main()
{
float a = 0.7;
if (a < 0.7f)
printf("C");
else
printf("C++");
}
Open
View answer
In the following C program there is a mistake related to function declaration and type conversion. Which extra statement should be added before main to make the program correct and avoid implicit declaration problems?
main()
{
int a;
a = f(10, 3.14);
printf("%d", a);
}
f(int aa, float bb)
{
return ((float) aa + bb);
}
Open
View answer
In the following C program, what is the error that prevents it from compiling correctly, and how should it be fixed?
main()
{
int a = 10;
void f();
a = f();
printf("%d", a);
}
void f()
{
printf("Hi");
}
Open
View answer
In modern C programming, will the following function definitions compile and work correctly as written, and why?
f1(int a, int b)
{
return (f2(20));
}
f2(int a)
{
return (a * a);
}
Open
View answer
In the following C code, the SWAP macro is intended to swap two integer variables, but it is defined incorrectly. After macro expansion, will this code compile successfully, and what is the main problem with the macro definition?
#define SWAP(a, b, c) (c t; t = a, a = b, b = t;)
main()
{
int x = 10, y = 20;
SWAP(x, y, int);
printf("%d %d", x, y);
}
Open
View answer
What would be the output of the following C program, assuming that:
- The 2D array a has base address 1002 in memory
- Each int occupies 2 bytes
- The compiler prints addresses in decimal
main()
{
int a[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
printf("%u %u %u",
a[0] + 1,
*(a[0] + 1),
*(*(a + 0) + 1));
}
Open
View answer
In a C program that uses POSIX threads on a Unix or Linux system, you see the linker error message "undefined reference to pthread_create". What is the correct way to fix this build error?
Open
View answer
In HTML, which tag is used to create drop-down list boxes and scrolling list boxes in a web form?
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.