0% found this document useful (0 votes)
24 views4 pages

Midterm

Uploaded by

timothybonario
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Midterm

Uploaded by

timothybonario
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Ignatius Timothy Bonario

Rafie Djajasoepena

07 March 2023

Midterm Computer Programming

1. For each of the following identifiers, indicate whether or not they are valid in C and for each give an exact
reason why.

a. Hello2You
Valid
Because the way how each of the character inputted are available in C
A-Z, 0-9

b. 2_Day_Is
Valid
Because the way how each of the character inputted are available in C
A-Z, 0-9, also underscore (but not in the beginning of the identifier)

c. _YOUR_Lucky
Invalid
Because it is started with an underscore “_” which makes this identifier invalid

d. Day:-)
Invalid
Because it contains a special character “:” which makes this identifier invalid

2. Which of the following are keywords in C?

a. while
b. constant
c. cases
d. switch

Only option “a” and “d” that are available in C, because “constant” and “cases” are not available in C.
The proof:
The bolded text in the compiler will work, meanwhile the non-bolded text will not be functional.
3. Assume we have declared three float variables a, b, and c.
What are the values of these three variables, if the code contain:

scanf(“%f%f%f”, &a, &b, &c); and the user enters

▪▪▪1.22.333.4444▪▪▪◊

(where ▪ represents a single space character and ◊ represents [Enter]). Explain you answer.

The variable “a” is 1.22 but printed as 1.220000 because it is the default digit length for float. And it will
not print any other numbers because the variable “a” itself is fulfilled.

The variable “b” is .333 but printed as 0.333000 because it is the default digit length for float.
It will not take 1.22 because it is already taken by “a”, and will not print any other numbers because the
variable “b” itself is fulfilled.

The variable “c” is .4444 but printed as 0.444400 because it is the default digit length for float.
It will not take the other numbers because they are already taken by “b” and “a”.

4. What is the output of each of the following:


use a ● to represent a space.

a. printf("%6.2f", 99.9);
The compiler gives an output of ●99.90
It is in float with two decimal points

b. printf("%-6.2f", 99.9);
The compiler gives an output of 99.90
It is in float with two decimal points

c. printf("%4d", 99);
The compiler gives an output of ●●99
It is in integer

5. Give the output of each of the following.

a) int i = 11, j = 22, k = 33;


printf(“%d ”, (i = j) && (j = k));
printf(“%d %d %d”, i, j, k);
The compiler gives the output of
(1 22 33 33)

b) int i = 10, j = 10, k = 10;


printf(“%d ”, ++i && ++j || ++k);
printf(“%d %d %d”, i, j, k);

The compiler gives the output of


(1 11 11 10)

6. What are the decimal equivalents of the following and show the steps:

a) 0102
This is an octal (started with “0”)
(1 x 8^2) + (0 x 8^1) + (2 x 8^0)
= 64 + 0 + 2
= 66

b) 0203
This is an octal (started with “0”)
(2 x 8^2) + (0 x 8^1) + (3 x 8^0)
= (2 x 64) + 0 + 3
= 131

c) 0304
This is an octal (started with “0”)
(3 x 8^2) + (0 x 8^1) + (4 x 8^0)
= (3 x 64) + 0 + 4
= 196

d) 0xa0b
This is a hexadecimal (started with “0x”)
(10 x 16^2) + (0 x 16^1) + (11 x 16^0)
= (10 x 256) + 0 + 11
= 2571

e) 0xb0c
This is a hexadecimal (started with “0x”)
(11 x 16^2) + (0 x 16^1) + (12 x 16^0)
= (11 x 256) + 0 + 12
= 2828
f) 0xc0d
This is a hexadecimal (started with “0x”)
(12 x 16^2) + (0 x 16^1) + (13 x 16^0)
= (12 x 256) + 0 + 13
= 3085

7. Which of the following are valid types in C and why?

a) long
b) long int
c) signed long int

= In here, “long” is detected as “long int” type (it is the shorter way to write “long int”). It is valid.
The “long int” and “signed long int” types are all valid and have the range of
-2,147,483,648 to 2,147,483,647
or –(2^31) to (2^31 – 1)

You might also like