Test Your Knowledge (Ch 5)

The following questions are based on what we have learned so far in Chapter 5 – Data Types in C. 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

Determine the output of the following C program (assume that the unsigned int is of size 4 bytes):

#include <stdio.h>

int main() {
    unsigned int var = 4294967296;
    printf("%u", var);
    return 0;
}
4294967296
0
-2147483647
None of the above
explanation

An unsigned integer with a size of 4 bytes can hold values up to 4294967295. If we try to assign a value of 4294967296 (which is one more than the maximum value), it will “roll back” to 0. So, in this case, the output will be 0.

Question 2

What is the output of the following code?

#include <stdio.h>

int main() {
    char c = -128;
    printf("%d", c);

    return 0;
}
-128
128
0
None of the above
explanation

The variable c is declared as a char and assigned the value -128. In C, a char is a signed type that can hold values from -128 to 127. When printing a char using the %d format specifier, it is treated as a signed integer and will be displayed as -128.

Question 3

Which of the following is NOT a valid declaration in C?
(A)  unsigned int i;
(B)  unsigned short i; 
(C)  unsigned short int i;
(D)  signed i;

(A) and (D)
(B) and (D)
Only (D)
All are valid
explanation

All are valid declarations in C. unsigned short i; can be used in place of unsigned short int i; and signed i; can be used in place of signed int i; or int i;

Question 4

Determine the output of the following C program:

#include <stdio.h>

int main() {
    int var = 0xf04;
    printf("%d", var);

    return 0;
}
0xf04
f04
3844
Compilation error
explanation

Hexadecimal values in C are represented by prefixing the hexadecimal value with 0x. As the format specifier in the printf function is %d, therefore, the decimal equivalent of the hexadecimal number f04 will be printed.

Question 5

How to make a constant of type long long?

By adding the prefix LL
By adding the suffix LL
By adding the prefix long long
None of the above
explanation

Refer this article for details – https://pumpedupbrains.com/tutorials/integer-constants/ 

Question 6

Which format specifier is used to print a value in the hexadecimal format?

%o
%h
%x
%d
explanation
Question 7

Determine the output of the following C program:

#include <stdio.h>

int main() {
    void var = 10;
    printf("%d", var);

    return 0;
}
Compilation error
10
0
None of the above
explanation

There is nothing like a void variable in C. The reason is that when a variable is declared, its type determines the size of the variable. Compiler has to allocate memory for the variable based on its type. A void variable means that its size is not defined which is illegal. Therefore, the compiler may throw an error.

Question 8

What is the output of the following C program? (Assume that the size of int is 4 bytes, size of float is 4 bytes, and size of double is 8 bytes)

#include <stdio.h>

int main() {
    printf("%d ", sizeof(5.5));
    return 0;
}
No output
Compilation error
8
4
explanation

Asking the sizeof(5.5) is the same as asking the sizeof(double). Note that the default type of a floating-point constant is double in C. Therefore, the correct output is 8.

Question 9

Determine the output of the following program:

#include <stdio.h>

int main() {
    float x = 5.67;
    double y = 5.67;
    
    if (x == y) {
        printf("You are great :D");
    } else {
        printf("Better luck next time :(");
    }
    return 0;
}
You are great 😀
Better luck next time 🙁
Nothing is printed
None of the above
explanation

When dealing with floating-point numbers (numbers with decimal points), the exact value stored in memory can be different from what we expect due to the limited precision of floating-point representation. For example, the following program demonstrates how the variables of type of float and double are storing different values in them. Even though we expect both variables to hold the same value, the program actually shows that they store different values.

#include <stdio.h>

int main() {
    float x = 5.67;
    double y = 5.67;
   
    printf("%.15f\n", x);
    printf("%.15f\n", y);
    return 0;
}

Output:

Question 10

Consider the following program:

#include <stdio.h>

int main() {
    int ch;
    while ((ch = getchar()) != ' ') {
        putchar(ch);
    }
    return 0;
}

Let’s say that the user input is “C is a wonderful language”. Can you determine the output of the above program with this input?

C is a wonderful language
An Integer variable cannot store a character. Hence, compilation error.
putchar() is not a function in C
C
explanation

Notice that within the parentheses of the while loop, the terminator used is a blank space. Hence, only ‘C’ is displayed on the screen.

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.