Computer StudyMCQ Computer ScienceUGC NET

C Programming Questions and Answers – Declarations

C Programming Questions and Answers – Declarations – 1

 

1.What is the output of this C code?

 

#include <stdio.h>

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;

}

a) Compile time error

b) 10 20

c) Undefined value

d) 10

 

View Answer

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?

#include <stdio.h>

int main()

{

const int i = 10;

int *ptr = &i;

*ptr = 20;

printf(“%d\n”, i);

return 0;

}

a) Compile time error

b) Compile time warning and printf displays 20

c) Undefined behaviour

d) 10

View Answer

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?

#include <stdio.h>

int main()

{

j = 10;

printf(“%d\n”, j++);

return 0;

}

a) 10

b) 11

c) Compile time error

d) 0

 

View Answer

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?

#include <stdio.h>

int main()

{

for (int k = 0; k < 10; k++);

return 0;

}

a) Yes

b) No

c) Depends on the C standard implemented by compilers

d) None of the mentioned

View Answer

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?

#include <stdio.h>

int main()

{

int k;

{

int k;

for (k = 0; k < 10; k++);

}

}

a) Yes

b) No

c) Depends on the compiler

d) Depends on the C standard implemented by compilers

View Answer

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)

View Answer

Answer:a

Explanation:It is legal in Java, not in C.

 

7.#include <stdio.h>

int main()

{

char *var = “Advanced Training in C by Sanfoundry.com”;

}

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

a) %f

b) %d

c) %c

d) %s

 

View Answer

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”;

 

View Answer

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

 

View Answer

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;

 

View Answer

Answer:d

Explanation:Array declarations are pointer declarations.

 

11.What is the output of this C code?

#include <stdio.h>

void main()

{

int k = 4;

float k = 4;

printf(“%d”, k)

}

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

 

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

 

View Answer

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

 

View Answer

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

 

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.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button