Multiple Choice Questions and Answers for Declarations in C Language

Here is a listing of C interview questions on “Declarations” along with answers, explanations and/or solutions:
1. What is the output of this C code?
  1.     #include 
  2.     void foo(const int *);
  3.     int main()
  4.     {
  5.         const int i = 10;
  6.         printf("%d ", i);
  7.         foo(&i);
  8.         printf("%d", i);
  9.  
  10.     }
  11.     void foo(const int *i)
  12.     {
  13.         *i = 20;
  14.     }
a) Compile time error
b) 10 20
c) Undefined value
d) 10

Answer:a

Explanation: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’
2. Comment on the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         const int i = 10;
  5.         int *ptr = &i;
  6.         *ptr = 20;
  7.         printf("%d\n", i);
  8.         return 0;
  9.     }
a) Compile time error
b) Compile time warning and printf displays 20
c) Undefined behaviour
d) 10

Answer:b

Explanation: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
3. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         j = 10;
  5.         printf("%d\n", j++);
  6.         return 0;
  7.     }
a) 10
b) 11
c) Compile time error
d) 0

Answer:c

Explanation: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.)
4. Does this compile without error?
  1.     #include 
  2.     int main()
  3.     {
  4.         for (int k = 0; k < 10; k++);
  5.             return 0;
  6.     }
a) Yes
b) No
c) Depends on the C standard implemented by compilers
d) None of the mentioned

Answer:c

Explanation: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
5. Does this compile without error?
  1.     #include 
  2.     int main()
  3.     {
  4.         int k;
  5.         {
  6.             int k;
  7.             for (k = 0; k < 10; k++);
  8.         }
  9.     }
a) Yes
b) No
c) Depends on the compiler
d) Depends on the C standard implemented by compilers

Answer:a

Explanation:There can be blocks inside block and within blocks variables have only block scope.

Output:
$ cc pgm5.c
6. Which of the following declaration is not supported by C?

a) String str;
b) char *str;
c) float str = 3e2;
d) Both (a) and (c)

Answer:a

Explanation:It is legal in Java, not in C.
7.
  1.     #include 
  2.     int main()
  3.     {
  4.         char *var = "Advanced Training in C by Sanfoundry.com";
  5.     }
Which of the following format identifier can never be used for the variable var?
a) %f
b) %d
c) %c
d) %s

Answer:a

Explanation:%c can be used to print the indexed position. %d can still be used to display its ASCII value. %s is recommended.
%f cannot be used.
8. Which of the following declaration is illegal?

a) char *str = “Best C programming classes by Sanfoundry”;
b) char str[] = “Best C programming classes by Sanfoundry”;
c) char str[20] = “Best C programming classes by Sanfoundry”;
d) char[] str = “Best C programming classes by Sanfoundry”;

Answer:d

Explanation:char[] str is a declaration in Java, not in C.
9. Which keyword is used to prevent any changes in the variable within a C program?

a) immutable
b) mutable
c) const
d) volatile

Answer:c

Explanation:const is a keyword constant in C program.
10. Which of the following is not a pointer declaration?

a) char a[10];
b) char a[] = {‘1’, ‘2’, ‘3’, ‘4’};
c) char *str;
d) char a;

Answer:d

Explanation:Array declarations are pointer declarations.
11. What is the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         int k = 4;
  5.         float k = 4;
  6.         printf("%d", k)
  7.     }
a) Compile time error
b) 4
c) 4.0000000
d) 4.4

Answer:a

Explanation: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
12. Which is false ?

a) A variable defined once can be defined again with different scope
b) A single variable cannot be defined with two different types in the same scope
c) A variable must be declared and defined at the same time
d) A variable refers to a location in memory

Answer:c

Explanation:It is not an error if the variable is declared and not defined. For example – extern declarations.
13. A variable declared in a function can be used in main

a) True
b) False
c) True if it is declared static
d) None of the mentioned

Answer:b

Explanation:Since the scope of the variable declared within a function is restricted only within that function,
the above statement is false.
14. The name of the variable used in one function cannot be used in another function

a) True
b) False
c) May be
d) None of the mentioned

Answer:b

Explanation:Since the scope of the variable declared within a function is restricted only within that function, the same name can be used to declare another variable in another function.

Related

Multiple Choice Questions 3523628204765124908

Post a Comment

emo-but-icon

item