77 Examples and Exercises of C Programming
77 Examples and Exercises of C Programming
77 Examples and Exercises of C Programming
Publisher
Page | i
77 EXAMPLES & EXERCISES OF C PROGRAMMING
Toutes droits réservés. Without the author's explicit written consent, no portion
of this book may be duplicated, saved for later retrieval, or converted into any form or by
any method, including mechanical, electronic, refilming and recording, etc.
Published by:
Politeknik Sultan Idris Shah,
Sungai Lang,
45100 Sungai Air Tawar,
Selangor Darul Ehsan.
No. Tel: 03 3280 6200
No. Fax: 03 3280 6400
Website: https://psis.mypolycc.edu.my/index.php/interaktif/
penerbitan @ Ts. Mohd At-Tarmizi Bin Abu Hassan.
Page | ii
77 EXAMPLES & EXERCISES OF C PROGRAMMING
I would like to acknowledgement the assistance and encouragement of our families, friends
and top management of Politeknik Sultan Idris Shah who have actively contributed to the
successful publication of this book.
Page | iii
77 EXAMPLES & EXERCISES OF C PROGRAMMING
Page | iv
77 EXAMPLES & EXERCISES OF C PROGRAMMING
Acknowledgements iii
Preface iv
Table of Contents v
Page | v
77 EXAMPLES & EXERCISES OF C PROGRAMMING 1
77 EXAMPLES OF C PROGRAMMING
1. SEQUENCE STRUCTURE
2. SELECTION STRUCTURE
23 - If under age.c source code
3. REPETITION STRUCTURE
4. ARRAY
50 - 1Array_Initializing.c source code
5. FUNCTION.
60 - 1Function_User_Defined.c source code
77 EXAMPLES & EXERCISES OF C PROGRAMMING 62
6. POINTER.
72 - Pointer.c source code
7. APPLICATIONS.
77 EXERCISES OF C PROGRAMMING
2. Algorithm is defined as
A. The sequence of steps required to provide a solution to a problem
B. The sequence to provide a solution to a case
C. The instructions to get the output
D. Combined the instructions in a program
3. Alphabetic and numeric combinations, however they must all begin with
an alphabet. Only the underscore (_) may be used as a special character.
This statement is referring to
A. Constants
B. Variables
C. Identifiers
D. Keywords
#include<stdio.h>
int main()
{
int i=1;
i= 2+2*i++;
printf(“%d”,i);
return 0;
}
A. 3
B. 4
C. 5
D. 6
6. Daniel wants to design a program to generate the eligible age to apply for a driving license.
Choose the appropriate control structure to solve problem above.
A. if - else
B. switch - case
C. for
D. do - while
A. 012345
B. 01234
C. 1234
D. 12345
10. If a is an integer variable, after modulus operation a = 5%2; will return a value
A. 0
B. 1
C. 2
D. 2.5
77 EXAMPLES & EXERCISES OF C PROGRAMMING 89
14. _________________ are closer to human languages and easier to understand. It is also used
to develop complex applications.
A. Machine Language
B. Intermediate Level Language
C. High-Level Language
D. Assembly Language
15. Below are the processes that take place in the pre-processor and compiler during a
compilation process EXCEPT?
A. Replace macro with code
B. Comments removal
C. Generates an executable file
D. Include header file
18. C programs are converted into machine language with the help of _________________.
A. an interpreter
B. an editor
C. a builder
D. a compiler
20. The vocabulary of commands understood by humans and can be converted into machine
language refers to the C ______________.
A. syntax
B. grammar
C. language
D. Semantics
22. The programming tool uses linked symbols to show the sequence of steps needed to solve a
programming problem.
A. Pseudocode
B. Flowchart
C. Input Process Output (IPO) Table
D. Grid Table
A. Terminal
B. On-page Connector
C. Off-page Connector
D. Subroutine
77 EXAMPLES & EXERCISES OF C PROGRAMMING 91
START
1. Get Num
2. While(Num>0)
2.1 Sum = Sum + Num
2.2 Num = Num + 1
1. Display Sum
STOP
A. Selection
B. Nested
C. Sequential
D. Looping
26. Does the following flowchart represent which control structure in C programming?
START
1. Read A, B
2. Calculate Sum = A + B
3. Display Sum
STOP
A. Selection
B. Sequential
C. Repetition
D. Nested
31. Identify which control structure does the following flowchart represent in C programming?
A. if selection structure
B. switch case selection structure
C. if else selection structure
D. nested if selection structure
34. Which of the following does NOT qualify as a valid bit representation?
A. 8 bit
B. 24 bit
C. 32 bit
D. 64 bit
36. What are the names of the entities whose values can be modified?
A. Tokens
B. Variables
C. Constants
D. Modules
37. If ‘a’ is an integer variable, then a = 5/2; will return a value of?
A. 2.5
B. 0
C. 2
D. 3
38. What should be the appropriate value returned to the operating system when a programme
has successfully run?
A. -1
B. 0
C. 1
D. Programs do not return a value.
40. What type of punctuation marks the start and end of a code block?
A. {}
B. ( )
C. [ ]
D. BEGIN and END
77 EXAMPLES & EXERCISES OF C PROGRAMMING 94
A. i, ii, iv, v
B. i, iv
C. ii, iii, v
D. ii, iv, v
46. Pick one of the following symbols that can be included in a variable name.
A. asterisk (*)
B. hashtag (#)
C. addition (+)
D. underscore (_)
A. i, ii and iii
B. i, iii, and iv
C. ii and iii
D. i, ii, iii, iv
A. Operator
B. Constant
C. Variable
D. Data type
54. To print out a and b below, which of the following printf() statement should be used?
#include <stdio.h>
void main ()
{
float a=3.14;
unsigned char b=’&’;
}
55. What is the output of the programme based on the flowchart below?
Start
Message to print:
If intGrade >= 70 No
“Failing grade”
Yes
Message to print:
“Passing grade”
Stop
58. How many different outcomes can be achieved with a single if-else statement?
A. 1
B. 2
C. 3
D. 4
77 EXAMPLES & EXERCISES OF C PROGRAMMING 98
59. The infinite looping CANNOT be avoided by a _______________ if the condition is missing in
a ‘for’ loop.
A. continue statement
B. go to statement
C. return statement
D. break statement
60. Which of the following statements are TRUE about while loop statements?
i. It is also known as exit controlled loop.
ii. It is an entry-controlled loop.
iii. The body of loop will be executed at least once even if the test condition is false.
iv. If test condition is initially false the body of loop is not executed at all.
A. i and iii
B. i and iv
C. ii and iii
D. ii and iv
A. Terminal
B. On-page connector
C. Subroutine
D. Off-page connector
77 EXAMPLES & EXERCISES OF C PROGRAMMING 99
#include<stdio.h>
int main()
{
int a=2;
if (a==2)
{
a=-a+2<1;
printf(“%d”,a);
}
else
{
break;
}
}
A. if
B. if else
C. nested if else
D. switch case
65. What must be done to prevent failing from one case to the next?
A. stop;
B. break;
C. end;
D. A semicolon
77 EXAMPLES & EXERCISES OF C PROGRAMMING 100
int x=0;
switch(x)
{
case 1: printf(“One”);break;
case 0: printf(“Zero”); break;
case 2: printf(“Hello World”);break;
}
A. One
B. Zero
C. Hello World
D. ZeroHello World
71. What is the final value of x when the code for (int x=0; x<10; x++) is executed?
A. 0
B. 1
C. 9
D. 10
72. What do you think will be the output of the code below?
#include<stdio.h>
void main()
{
int i;
for(i=0;i<10;i++)
printf("%d ",i);
}
A. 0123456789
B. 012345678
C. 123456789
D. 123456789
73. Which of the following statements are TRUE about Do-While loop statements?
i. It is also known as exit controlled loop
ii. It is an entry controlled loop.
iii. The body of loop will be executed at least once even if the test condition is false.
iv. If test condition is initially false the body of loop is not executed at all.
A. i and iii
B. i and iv
C. ii and iii
D. ii and iv
77 EXAMPLES & EXERCISES OF C PROGRAMMING 102
74. In the code below, how many times is the value of x tested?
#include <stdio.h>
int main()
{
int x = 0;
while (x<3)
{
x++;
printf("DEC20012 Is Fun!\n");
}
}
A. 2
B. 4
C. 3
D. 1
A. 2
B. 4
C. 6
D. 8
Answer.
1. C 41. D
2. A 42. C
3. C 43. C
4. B 44. C
5. D 45. C
6. A 46. D
7. A 47. B
8. D 48. C
9. B 49. A
10. B 50. D
11. D 51. B
12. A 52. A
13. C 53. C
14. C 54. D
15. C 55. C
16. B 56. B
17. A 57. B
18. D 58. B
19. C 59. A
20. D 60. D
21. C 61. D
22. B 62. B
23. C 63. D
24. D 64. A
25. D 65. B
26. C 66. B
27. D 67. D
28. B 68. C
29. C 69. A
30. B 70. A
31. C 71. D
32. B 72. A
33. B 73. A
34. B 74. B
35. D 75. C
36. B 76. D
37. C 77. C
38. B
39. C
40. A
77 EXAMPLES & EXERCISES OF C PROGRAMMING
1. Gookin, D. (2013). Beginning Programming with C for Dummies. John Wiley & Sons.
7. Programiz. (n.d.). Parewa Labs Pvt. Ltd. Retrieved from Learn C Programming:
https://www.programiz.com/c-programming
Page | vi