Test Your Knowledge (Ch 3)

The following questions are based on what we have learned so far in Chapter 3 – The Printf Function. To test your understanding, try answering the following questions. The explanations are also provided to each question so you can always check the correctness of your answers. All the best 👍


Question 1

The %d format specifier is used as a placeholder for

decimal
binary
string
None of the above
explanation

Option (a) is the correct option because d in %d stands for decimal (integer) and therefore, it is used as a placeholder for decimal value.

Question 2

The getchar() is used to 

Receive a string from the user
Show the blinking cursor
Receive a character from the user
None of the above
explanation

Option (c) is the correct option. The getchar() function is used to receive a single character from the user.

Question 3

Which format specifier is used to print a floating-point number in printf? 

%d
%f
%fpn
None of the above
explanation

Option (b) is the correct option. %f is used as a placeholder for a floating-point number in printf.

Question 4

What is the difference between “%d” and “%i” format specifiers in the printf function?

No difference
%d is better than %i
%d is a placeholder for decimal values and %i is a placeholder for negative integers
None of the above
explanation

Option (a) is the correct option. Both %d and %i are interchangeable format specifiers in the printf function. Functionally, there is no difference between them, and they can be used interchangeably as placeholders for integers or decimal values.

Question 5

Determine the output of the following program:

#include <stdio.h>

int main() {
    char ch1 = 'A', ch2 = 'B';
    printf("%c %c %c %c %c", ch1, ch1+1, ch1+2, ch2, ch2+1);

    return 0;
}
65 66 67 66 67
A B C D E
A B C B C
None of the above
explanation

Option (c) is the correct option. 
As ch1 is holding the character ‘A’, and the placeholder is %c, therefore character ‘A’ will be printed first. Then, 1 is added to ch1. This time we need to have a look at the ASCII table. 

In the ASCII table, characters are arranged in chronological order, and their ASCII codes are incremented in a sequential manner. For instance, ‘A’ has an ASCII code of 65, ‘B’ has 66, ‘C’ has 67, and so on.

Returning to our question, since ch1 is ‘A’ with an ASCII code of 65, adding 1 to it yields 66, which corresponds to the ASCII code of the letter ‘B’. Therefore, the next printed character will be ‘B’. By following this pattern, we can determine the characters that will be printed subsequently.

Question 6

How many whitespaces are added before the number 10 after running the following program:

#include <stdio.h>

int main() {
    int x = 10;
    printf("%8d", x);
    return 0;
}
8
10
3
6
explanation

Option (d) is the correct option. 
A total of 6 whitespaces are added at the beginning of the number 10 because the minimum field width is set to 8. Out of these 8 spaces, 2 spaces are allocated for the digits 1 and 0, and the remaining 6 spaces are allocated for the leading whitespaces.

The output of the given program is

      10
Question 7

Determine the output of the following C program:

#include <stdio.h>

int main() {
    int day = 25, month = 5, year = 2023;
    printf("Today's date is: %2d/%2d/%4d", day, month, year);

    return 0;
}
Today’s date is: 2552023
Today date is: 25 52023
25/ 5/2023
Today’s date is: 25/ 5/2023
explanation

Option (d) is the correct option.
The program is capable enough to print the current date in the format “dd/mm/yyyy”. As month is a single digit 5, and the placeholder for month is %2d, a leading whitespace will be added in front of 5.

Therefore the correct output is

Today’s date is: 25/ 5/2023
Question 8

Determine the output of the following C program:

#include <stdio.h>

int main() {
    int day = 25, month = 5, year = 2023;
    printf("Today's date is: %02d/%02d/%04d", day, month, year);

    return 0;
}
Today’s date is: 25/ 5/2023
Today’s date is: 25/5/2023
Today’s date is: 025/05/02023
Today’s date is: 25/05/2023
explanation

Option (d) is the correct option.
In the given program, one should focus on the format specifier being used. %02d is a format specifier that specifies the placeholder for an integer with a minimum field width of 2 digits. The leading 0 means that if the number has less than 2 digits, leading zeros will be added.

Therefore the correct output is

Today’s date is: 25/05/2023
Question 9

Determine the output of the following C program:

#include <stdio.h>

int main() {
    float percentage = 0.75;
    printf("Success rate: %.2f%%", percentage * 100);

    return 0;
}
Success rate: 75.00
Success rate: 75%
Success rate: 75.00%
Success rate: 75.0%
explanation

Option (c) is the correct option. 
By multiplying 0.75 by 100, we obtain 75. The format specifier %.2f is used to display the value 75 as a floating-point number with two decimal places. The two percentage signs at the end are included to display a single percentage sign in the output.

Therefore, the correct output will be:

Success rate: 75.00%
Question 10

Determine the output of the following C program:

#include <stdio.h>

int main() {
    float value = 12345.6789;
    printf("Scientific notation: %.2e", value);

    return 0;
}
1.23e+04
123.45e+02
12.345e+03
None of the above
explanation

Option (a) is the correct option. 
%e is used to display the number in scientific notation. ‘.2’ specifies that the floating-point number should be displayed with two digits after the decimal point. Hence, we will get 1.23e+04 which is the same as saying 1.23 multiplied by 10 raised to the power of 4. 

Your score is out of



Leave a comment

Leave a comment

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

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.