Sail E0 Webinar

MCQs

Total Questions : 13 | Page 1 of 2 pages
Question 1. What is the output of this C code?
void foo(const int *);
int main()
{
const int i = 10;
printf("%d ", i);
foo(&i);
printf("%d", i);
}
void foo(const int *i)
{
*i = 20;
}
  1.    Compile time error
  2.    10    20
  3.    Undefined value
  4.    10
 Discuss Question
Answer: Option A. -> Compile time error


Cannot change a const type value.
Output:
$ cc pgm1.c
pgm1.c: In function 'foo':
pgm1.c:13: error: assignment of read-only location '*i'


Question 2. Comment on the output of this C code?
int main()
{
const int i = 10;
int *ptr = &i;
*ptr = 20;
printf("%d\n", i);
return 0;
}
  1.    Compile time error
  2.    Compile time warning and printf displays 20
  3.    Undefined behaviour
  4.    10
 Discuss Question
Answer: Option B. -> Compile time warning and printf displays 20


Changing const variable through non-constant pointers invokes compiler warning
Output:
$ cc pgm2.c
pgm2.c: In function 'main':
pgm2.c:5: warning: initialization discards qualifiers from pointer target type
$ a.out
20


Question 3. Does this compile without error?
int main()
{
for (int k = 0; k < 10; k++);
return 0;
}
  1.    Yes
  2.    No
  3.    Depends on the C standard implemented by compilers
  4.    None of the mentioned
 Discuss Question
Answer: Option C. -> Depends on the C standard implemented by compilers


Compilers implementing C90 does not allow this but compilers implementing C99 allow it.
Output:
$ cc pgm4.c
pgm4.c: In function 'main':
pgm4.c:4: error: 'for' loop initial declarations are only allowed in C99 mode
pgm4.c:4: note: use option -std=c99 or -std=gnu99 to compile your code


Question 4. What is the output of this C code?
int main()
{
j = 10;
printf("%d\n", j++);
return 0;
}
  1.    10
  2.    11
  3.    Compile time error
  4.    0
 Discuss Question
Answer: Option C. -> Compile time error


Variable j is not defined.
Output:
$ cc pgm3.c
pgm3.c: In function 'main':
pgm3.c:4: error: 'j' undeclared (first use in this function)
pgm3.c:4: error: (Each undeclared identifier is reported only once
pgm3.c:4: error: for each function it appears in.)


Question 5. Does this compile without error?
int main()
{
int k;
{
int k;
for (k = 0; k < 10; k++);
}
}
  1.    Yes
  2.    No
  3.    Depends on the compiler
  4.    Depends on the C standard implemented by compilers
 Discuss Question
Answer: Option A. -> Yes


There can be blocks inside block and within blocks variables have only block scope.
Output:
$ cc pgm5.c


Question 6. Which of the following declaration is not supported by C?
  1.    String str;
  2.    char *str;
  3.    float str = 3e2;
  4.    Both (a) and (c)
 Discuss Question
Answer: Option A. -> String str;


It is legal in Java, not in C.


Question 7. Which of the following is not a pointer declaration?
  1.    char a[10];
  2.    char a[] = {'1', '2', '3', '4'};
  3.    char *str;
  4.    char a;
 Discuss Question
Answer: Option D. -> char a;


Array declarations are pointer declarations.


Question 8. Which of the following declaration is illegal?
  1.    char *str = Best C programming classes by AllIndiaExams";
  2.    char str[] = Best C programming classes by AllIndiaExams";
  3.    char str[20] = Best C programming classes by AllIndiaExams";
  4.    char[] str = Best C programming classes by AllIndiaExams";
 Discuss Question
Answer: Option A. -> char *str = Best C programming classes by AllIndiaExams";


char[] str is a declaration in Java, not in C.


Question 9. Which keyword is used to prevent any changes in the variable within a C program?
  1.    immutable
  2.    mutable
  3.    const
  4.    volatile
 Discuss Question
Answer: Option C. -> const


const is a keyword constant in C program.


Question 10. What is the output of this C code?
void main()
{
int k = 4;
float k = 4;
printf("%d", k)
}
  1.    Compile time error
  2.    4
  3.    4.0000000
  4.    4.4
 Discuss Question
Answer: Option A. -> Compile time error


Since the variable k is defined both as integer and as float, it results in an error.
Output:
$ cc pgm8.c
pgm8.c: In function 'main':
pgm8.c:5: error: conflicting types for 'k'
pgm8.c:4: note: previous definition of 'k' was here
pgm8.c:6: warning: format '%d' expects type 'int', but argument 2 has type 'double'
pgm8.c:7: error: expected ';' before '}' token


Latest Videos

Latest Test Papers