[go: up one dir, main page]

100% found this document useful (1 vote)
467 views9 pages

Solutions of Homework 1

This document contains the solutions to homework problems for an Introduction to Programming in C course. It includes the student's name and ID at the top, followed by 30 multiple choice or short answer programming questions and their answers.

Uploaded by

okay g
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
100% found this document useful (1 vote)
467 views9 pages

Solutions of Homework 1

This document contains the solutions to homework problems for an Introduction to Programming in C course. It includes the student's name and ID at the top, followed by 30 multiple choice or short answer programming questions and their answers.

Uploaded by

okay g
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/ 9

Introduction to Programming in C

Homework 1 Solution
BT ech(ET C), 1st Semester

Name: PRANENDU BIKASH PRADHAN

Student-ID: B219040

Due Date: 20 September, 2019

Solutions

1. The purpose of the header files, such as stdio.h, is to store a programs


source code.
•True
•False
Answer: False

2. Any valid printable ASCII character can be used in an identifier.


•True
•False
Answer: False

3. The C standard function that receives data from the keyboard is printf.
•True
•False
Answer: False

4. Which of the following statements about structure of a C program is


false?
a. A C program starts with a global declaration section
b. Declaration section contains instructions to the computer
c. Every program must have at least one function
d. One and only one function may be named main
e. Within each function there is a local declaration section
Answer: b

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

6. Which of the following identifiers is not valid?


a. option
b. amount
c. sales_amount
d. salesAmount
e. $salesAmount
Answer: e.

7. Which of the following is not a datatype?


a. char
b. float
c. int
d. logical
e. void
Answer: d.

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

9. Which of the following statements about a constant is true?


a. Character constants are coded using double quotes.
b. It is impossible to tell the computer that a constant should be float or a
long double.
c. Like variables, constants have a type and may be named.

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

10. The _______ conversion specification is used to read or write a short


integer.
a. %c
b. %d
c. %f
d. %hd
e. %lf
Answer: d

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

12. The ____________ function reads data from the keyboard.


a. displayf
b. printf
c. read
d. scanf
e. write
Answer: d.

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.

14. Which of the following is not character constant in C?


a. c
b. bb
c. c
d. ?
e.
Answer: c.

15. Which of the following is not an integer constant in C?


a. -320
b. +45
c. -31.80
d. 1456
e. 2,456
Answer: c

16. Which of the following is not a floating-point constant in C?


a. 45.6
b. -14.05
c. a
d. pi
e. 40
Answer: c

17. What is the type of each of the following constants?


a. 15
Answer: Integer constant
b. -14.24
Answer: Floating point constant
c. b
Answer: Character constant
d. 1
Answer: String constant
e. 16
Answer: String constant

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.

19. What is type of the following constants?


a. 7
Answer: String constant
b. 3
Answer: Integer constant
c. 3.14159
Answer: String constant
d.2
Answer: Character constant
e. 5.1
Answer: Floating point constant

20. What is the type of the following constants?


a. Hello
Answer: String
b. 15L
Answer: Long integer
c. 8.5L
Answer: Double
d. 8.5f
Answer: Float
e. \a
Answer: Character

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 ) ;

23. Find the errors in the following program, if any.

1 // This program does nothing


2 int main
3 {
4 return 0;
5

6 }
Correct Program:

6
1 // This program does nothing
2 int main ()
3 {
4 return 0;
5 }

24. Find the errors in the following program, if any.

1 # include ( stdio . h )
2 int main ( void ) {
3 print ( " Hello World " ) ;
4 return 0;
5 }

Correct Program;

1 # include < stdio .h >


2 int main ( void ) {
3 print ( " Hello World " ) ;
4 return 0;
5 }

25. Find the errors in the following program, if any.

1 # include < stdio >


2 int main ( void ) {
3 printf ( ’ We are to learn correct ’) ;
4 printf ( ’C language is here !! ’)
5 return 0;
6 }

Correct Program;

1 # include < stdio .h >


2 int main ( void ) {
3 printf ( " We are to learn correct " ) ;
4 printf ( " C language is here !! " )
5 return 0;
6 }

26. Find the errors in the following program, if any.

1 /* This is the program in which some


2 * errors in it need to be corrected
3 */
4 int main ( void ) {
5 // Local Declaration

7
6 integer a ;
7 floating - point b ;
8 character c ;
9 // Statements
10 printf ( " The end of the program . " )
11 return 0;
12 }

27. Find the errors in the following program, if any.

1 /* This is the program in which some


2 * errors in it need to be corrected
3 */
4 int main ( void ) {
5 // Local Declaration
6 a int ;
7 b float , double ;
8 c , d char ;
9 // Statements
10 printf ( " The end of the program . " )
11 return 0;
12 }

28. Find the errors in the following program, if any.

1 /* This is the program in which some


2 * errors in it need to be corrected
3 */
4 int main ( void ) {
5 a int ;
6 b : c : d char ;
7 d , e , f double float ;
8 // Statements
9 printf ( " The end of the program . " )
10 return 0;
11 }

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

You might also like