Log In Register

Units


Declarations - Questions and Answers

 

Practice these Declarations under C Programming walk-in interview Questions/ examination questions with best tricks and short cuts with solution. Student (candidate) who want to crack the walk in interview, competitive exams and want to find short cuts and tricks to solve questions on Declarations for following purpose.


– Anyone who is wishing to increase their perfect knowledge of Declarations of C Programming.
– Anyone who is preparing for aptitude test and increase his in depth aptitude knowledge C Programming specifically Declarations.
– Anyone who is preparing for interviews (competitive examinations, govt examinations and bank examinations, off-campus or on campus interviews, walk-in interview and company interviews) having questions on Declarations under C Programming.

– Anyone who is preparing for aptitude test or competitive exam or entrance exam.
– Anyone who is preparing for entrance examinations and other competitive examinations.
– All Experienced, Freshers and Students.

Here is list of Questions and Answers covering all key area of  Declarations topic of C Programming:

 

1.

Does this compile without error?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int k;
  5.         {
  6.             int k;
  7.             for (k = 0; k < 10; k++);
  8.         }
  9.     }

Answer: Option D

Explanation:

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

2.

What is the output of this C code?

  1.     #include <stdio.h>
  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.     }

Answer: Option D

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’

3.

Which of the following declaration is illegal?

Answer: Option A

Explanation:

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

4.

Comment on the output of this C code?

  1.     #include <stdio.h>
  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.     }

Answer: Option C

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

5.

Which of the following is not a pointer declaration?

Answer: Option A

Explanation:

Array declarations are pointer declarations.

6.

A variable declared in a function can be used in main

Answer: Option C

Explanation:

Since the scope of the variable declared within a function is restricted only within that function,
the above statement is false.

7.

Does this compile without error?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         for (int k = 0; k < 10; k++);
  5.             return 0;
  6.     }

Answer: Option A

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

8.

What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         j = 10;
  5.         printf("%d\n", j++);
  6.         return 0;
  7.     }

Answer: Option B

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.)

9.

The name of the variable used in one function cannot be used in another function

Answer: Option A

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.

10.

Which is false ?

Answer: Option B

Explanation:

It is not an error if the variable is declared and not defined. For example – extern declarations.

11.

Which of the following declaration is not supported by C?

Answer: Option C

Explanation:

It is legal in Java, not in C.

12.

 

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *var = "Advanced Training in C by testmirror.com";
  5.     }

Which of the following format identifier can never be used for the variable var? 

Answer: Option D

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.

13.

What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int k = 4;
  5.         float k = 4;
  6.         printf("%d", k)
  7.     }

Answer: Option D

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 ‘}&rsquo

14.

Which keyword is used to prevent any changes in the variable within a C program?

Answer: Option B

Explanation:

const is a keyword constant in C program.