Solutions of Homework 1
Solutions of Homework 1
Homework 1 Solution
BT ech(ET C), 1st Semester
Student-ID: B219040
Solutions
3. The C standard function that receives data from the keyboard is printf.
•True
•False
Answer: False
1
5. Which of the following statements about block comments is false?
a. Comments are internal documentation for programmers.
b. Comments are used by the pre processor to help format the program.
c. Comments begin with a /* token.
d. Comments cannot be nested.
e. Comments end with */ token.
Answer: b
8. The code that establishes the original value for a variable is known as
a(n):
a. assignment
b. constant
c. initializer
d. originator
e. value
Answer: c
2
d. Only integer values can be used in a constant.
e. The values of a constant may be changed during a programs execution.
Answer: c
11. To print data left justified, you would use a _______ in the conversion
specification.
a. flag
b. precision
c. size
d. width
e. width and precision
Answer: b
13. One of the most common errors for new programmers is forgetting to
use the address of operator for variables in a scanf statement. What is the
address operator?
a. The address modifier (@) in the conversion specification
b. The ampersand ()
c. The caret (^)
d. The percent (%)
e. The pound sign(#)
3
Answer:b.
4
18. Which of the following is not a valid identifier in C?
a. A3
b. 4A
c. if
d. IF
e. tax-rate
Answer: b. and e.
21. Which of the following identifiers are valid and which are invalid?
Explain your answer.
a. num
Answer: Valid as it has only lower case letters.
b. num2
Answer: Valid as it has only lower case letters and number.
5
c. 2dNum
Answer: Not valid as it starts with a number.
d. 2d_Num
Answer: Not valid as it starts witha number.
e. num#2
Answer: Not valid as it includes special character.
f. num-2
Answer: Not valid as it includes - .
g. num 2
Answer: Not valid as it includes space.
h. num_2
Answer: Valid.
i. _num2
Answer: Valid as it starts with an underscore.
j. _num_2
Answer: Valid as it includes only lower case letters and underscore.
22. What is the output from the following program fragment? To show
your output draw a grid of at least 8 lines with at least 15 characters per
line.
1 // Local declarations
2 int x = 10
3 char w = ’Y ’
4 float z = 5.1234
5 // Statements
6 printf ( " \ nFirst \ nExample \ n : " ) ;
7 printf ( " %5 d \n , w is % c \ n " , x , w ) ;
8 printf ( " \ nz is %8.2 f \ n " , z ) ;
6 }
Correct Program:
6
1 // This program does nothing
2 int main ()
3 {
4 return 0;
5 }
1 # include ( stdio . h )
2 int main ( void ) {
3 print ( " Hello World " ) ;
4 return 0;
5 }
Correct Program;
Correct Program;
7
6 integer a ;
7 floating - point b ;
8 character c ;
9 // Statements
10 printf ( " The end of the program . " )
11 return 0;
12 }
29. Code the variable declarations for each of the following: •a character
variable named option
•an integer variable, sum, initialized to 0
•a floating-point variable, product, initialized to 1
•a short integer variable named code
•a constant named salesTax initialized to .0825
•a floating-point named sum of size double initialized to 0
30. Write a statement to print the following line. Assume the total value
8
is contained in a variable named cost. The sales total is: $ 172.53
31. Write a program that uses four print statements to print the pattern
of asterisks shown below.
******
******
******
******
32. Write a program that uses four print statements to print the pattern
of asterisks shown below.
*
**
***
****
33. Write a program that defines five integer variables and initializes
them to 1, 10, 100, 1000 and 10000. It then prints them on a single line
separated by space characters using the decimal conversion code (%d), and
on the next line with the float conversion code (%f). Note the difference
between the results. How do you explain them?
34. Write a program that prompts the user to enter a quantity and a
cost. The values are to be read into an integer named quantity and a float
named unitPrice. Define the variables, and use only one statement to read
the values. After reading the values, skip one line and print each value with
an appropriate name on a separate line.
Ans.
35. Write a program that prompts the user to enter an integer and then
prints the integer first as a character, then as decimal, and finally as a float.
Use separate print statements. A sample run is shown below.
The number as a character : K
The number as a decimal : 75
The number as a float : 0.000000