Machine-translated by Google. Questions are authored in English.
Top

C Constants Questions and Answers

41.

What is the correct way to round offx, a float, to an int value?

Answer: A

Correct answer: y=(int)(x+0.5)

Adding 0.5 before truncating turns the cast's truncation into rounding: a fractional part of 0.5 or more carries the value past the next integer, while a smaller one does not.

The parentheses matter. (int)x + 0.5 casts first and then adds, so the 0.5 is never rounded away and the result is not an integer at all. And int(x+0.5) is C++ function-style casting, not valid C. Note the technique fails for negative values, where x - 0.5 would be needed, or round() from math.h.

Please log in or register to report this question.

42.

What is the output of this C code?

int main()
{
printf("C programming %s", "Class by\n%s AllIndiaExams", "SUPER");
}

 

Answer: C

This program has only one %s within first double quotes, so it does not read the string “SUPER”.
The %s along with the AllIndiaExams is not read as a format modifier while new line character prints the new line.

Please log in or register to report this question.

43.

As soon as a pointer variable is freed, its value 

Answer: D

Correct answer: remains the same

free releases the storage but does nothing to the pointer, which is passed by value. Its bits are unchanged, so it still holds the address of memory that no longer belongs to the program - a dangling pointer.

This is exactly what makes use-after-free so dangerous: the pointer looks valid, may still appear to work while the freed block is untouched, and fails unpredictably once the allocator reuses it. The conventional discipline is to set the pointer to NULL immediately after freeing, so that misuse fails loudly.

Please log in or register to report this question.

Answer: D

Correct answer: all of these

All three statements describe an external variable. It has file scope and external linkage, so it is accessible from every function in the program, including functions in other translation units that declare it with extern.

It has a declaration, and it has static storage duration, so if no initialiser is given it is zero-initialised before the program starts rather than holding a garbage value. That last property distinguishes it sharply from an automatic variable, whose uninitialised value is indeterminate.

Please log in or register to report this question.

45.

Which of the following comments regarding the reading of a string, using scanf(with optional) and get is true?

Answer: C

Correct answer: Scanf is delimited by blank, while gets is not

scanf with %s stops at the first whitespace, so it reads one word however much was typed. gets reads until a newline, so it takes the whole line including embedded spaces.

That difference makes them anything but interchangeable - reading a name with scanf("%s") silently captures only the first word. It is worth adding that gets cannot limit the input to the buffer size and was removed from the language in C11; fgets is the correct replacement.

Please log in or register to report this question.

46.

For the following code snippet:

char *str = "Sanfoundry.com\0" "training classes";
The character pointer str holds reference to string:

Answer: B

Correct answer: Sanfoundry.com\0training classes

Adjacent string literals are concatenated at translation time, so the two become one literal with the \0 retained as a byte inside it and a further terminating null appended at the end.

The pointer refers to that whole storage. Printing it with %s would show only Sanfoundry.com, since printf stops at the first null - but what str holds a reference to is the complete concatenated sequence, which is what the question asks about.

Please log in or register to report this question.

Answer: D

Correct answer: Both (a) and (b)

(a) is true: C really has only one-dimensional arrays - int a[3][4] is an array of 3 objects, each of type int[4]. (b) says the same thing from the other side: an element may itself be an array. The third statement is false; elements are always contiguous, which is exactly what makes subscripting work.

Please log in or register to report this question.

Answer: D

Correct answer: All of the above

All three statements describe external variables. Because any function can read and write them, they provide a channel of communication in both directions between functions without passing arguments or returning values.

Their scope runs from the point of definition to the end of the translation unit, and beyond it in files that declare them extern. Having static storage duration, they are zero-initialised if no initialiser is given. The convenience is real but so is the cost: shared mutable state is the hardest kind of code to reason about.

Please log in or register to report this question.

49.

What is the output of this C code?

#include <stdio.h>
    int main()
    {
        printf("sanfoundry\rclass\n");
        return 0;
    }

 

Answer: C

Correct answer: classundry

The carriage return moves the cursor back to the start of the line without advancing it, so class is written over the beginning of sanfoundry. The five characters of class replace sanfo, leaving undry visible behind them.

The result on a terminal is classundry. This is the practical difference between \r and \r\n: alone, the carriage return overwrites; followed by a newline it simply ends the line, which is why the two together are a line ending on some systems.

Please log in or register to report this question.

50.

The program fragment
int i = 263 ;
putchar (i) ;
prints

Answer: C

Correct answer: rings the bell

putchar takes an int but converts it to unsigned char before writing, so only the low-order byte survives:

\[ 263 \bmod 256 = 7 \]

Character code 7 is BEL, the alert character, so the terminal sounds its bell rather than printing anything. The lesson is general: passing a value outside the range of unsigned char to putchar is not an error but a silent truncation, and the character actually written is whatever the low byte encodes.

Please log in or register to report this question.

Loading…
Tags: C Constants Questions and Answers || C Constants MCQ Questions and Answers || C Constants GK Questions and Answers || C Constants GK MCQ Questions || C Constants Multiple Choice Questions and Answers || C Constants GK || GK on C Constants || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming