C Programming Questions and Answers – Declarations – 2

«
»
Sanfoundry’s 1000+ Interview Questions & Answers on C help anyone preparing for TCS and other companies C interviews. One should practice these 1000+ interview questions and answers continuously for 2-3 months to clear TCS interviews on C Programming language.

Here is a listing of C questions and puzzles on “Declarations” along with answers, explanations and/or solutions:

1. 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”;
View Answer

Answer: d
Explanation: char[] str is a declaration in Java, but not in C.
advertisement

2. Which keyword is used to prevent any changes in the variable within a C program?
a) immutable
b) mutable
c) const
d) volatile
View Answer

Answer: c
Explanation: const is a keyword constant in C program.

3. 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;
View Answer

Answer: d
Explanation: Array declarations are pointer declarations.
advertisement
advertisement

4. What will be the output of the following C code?

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

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

5. Which of the following statement 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
View Answer

Answer: c
Explanation: It is not an error if the variable is declared and not defined. For example – extern declarations.
advertisement
advertisement

6. 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
View Answer

Answer: b
Explanation: Since the scope of the variable declared within a function is restricted only within that function, so the above statement is false.

7. The name of the variable used in one function cannot be used in another function.
a) True
b) False
View Answer

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

Sanfoundry Global Education & Learning Series – C Programming Language.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter