This book is dedicated to all students of Electrical Engineering at the polytechnic who take the subject DEC20012 – Fundamental Programming. The content is designed to enhance C programming understanding and skills through variations of examples and exercises. “Practice makes perfect!”
4. Page | iii
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.
Ts. Mohd At-Tarmizi Bin Abu Hassan
5. Page | iv
77 EXAMPLES & EXERCISES OF C PROGRAMMING
This book is dedicated to all students of
Electrical Engineering at the polytechnic who
take the subject DEC20012 – Fundamental
Programming. The content is designed to
enhance C programming understanding and
skills through variations of examples and
exercises. “Practice makes perfect!”
6. Page | v
77 EXAMPLES & EXERCISES OF C PROGRAMMING
iii
iv
v
1-16
17-35
36-49
50-60
61-77
78-79
80-86
Acknowledgements
Preface
Table of Contents
77 Examples of C Programming - Sequences Structure
77 Examples of C Programming - Selection Structure
77 Examples of C Programming - Repetition Structure
77 Examples of C Programming - Array
77 Examples of C Programming - Function
77 Examples of C Programming - Pointer
77 Examples of C Programming - Applications
77 Exercices of C Programming
References and Further Studies
87-103
vi
7. 1
77 EXAMPLES & EXERCISES OF C PROGRAMMING
77 EXAMPLES OF C PROGRAMMING
1. SEQUENCE STRUCTURE
1 - HelloWorld.c source code
1 - HelloWorld.c output file
2 - WelcomeToJKEPSIS.c source code
2 - WelcomeToJKEPSIS.c output file
92. 86
77 EXAMPLES & EXERCISES OF C PROGRAMMING
77 - Ohms Law Calculator.c output file
93. 87
77 EXAMPLES & EXERCISES OF C PROGRAMMING
77 EXERCISES OF C PROGRAMMING
1. The term 'programming language' is defined as
A. The language which machines communicate with machines
B. The language which machines communicate with humans
C. The language which humans communicate with computers
D. The special language for machines
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
4. Select the program's output from the following:
A. 3
B. 4
C. 5
D. 6
5. In a "switch-case" statement, which of the following cannot be checked?
A. enum
B. integer
C. character
D. float
#include<stdio.h>
int main()
{
int i=1;
i= 2+2*i++;
printf(“%d”,i);
return 0;
}
94. 88
77 EXAMPLES & EXERCISES OF C PROGRAMMING
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
7. "Repeat the section of your program based on a specified number of times."
The above statement is referring to
A. for
B. if
C. if - else
D. switch case
8. What will the program's results be?
A. 0 1 2 3 4 5
B. 0 1 2 3 4
C. 1 2 3 4
D. 1 2 3 4 5
9. Identify the elements in planning process.
i. Calculation
ii. Process
iii. Input
iv. Output
v. Progress
A. i, ii, and iii only
B. ii, iii and iv only
C. iii and iv only
D. All of the above.
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
#include<stdio.h>
int main ()
{
int i=0;
for (i=1; i<=5; i++)
{printf ("%d", i);}
return 0;
}
95. 89
77 EXAMPLES & EXERCISES OF C PROGRAMMING
11. What is the definition of programme?
A. A person, who designs, writes and tests computer programs.
B. A computer tool that allows a programmer to write commands in a format.
C. A translated code that the computer can understand and execute.
D. A specific set of ordered operations for a computer to perform.
12. The programming language structure or grammar refers to the C ____________________.
A. Syntax
B. Grammar
C. Language
D. Semantics
13. C programming is considered a ___________________.
A. Low-Level Language
B. Intermediate Level Language
C. High-Level Language
D. Assembly Language
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
16. Below are acceptable definitions of a programme EXCEPT?
A. A well-organized set of guidelines that specify to the computer what to do and how
to do it.
B. A numbered list of instructions to perform some task usually used to outline the
general steps in an algorithm without having to write the actual codes.
C. A set of source code written in computer programming language meant for a specific
task and executed in certain sequences.
D. A text file containing sequences of C semantics put together according to C syntax.
96. 90
77 EXAMPLES & EXERCISES OF C PROGRAMMING
17. _________________ contains only numerical information and is difficult to be understood
by a human.
A. Machine Language
B. Intermediate Level Language
C. High-Level Language
D. Assembly Language
18. C programs are converted into machine language with the help of _________________.
A. an interpreter
B. an editor
C. a builder
D. a compiler
19. Which of the following statements BEST describes a programmer?
A. A person who designs computer programs.
B. A person who composes instructions for the computer system.
C. A person who designs, writes, and test computer programs.
D. A person who test computer programs.
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
21. Listed below are the types of programming EXCEPT?
A. Modular programming
B. Structured programing
C. High-Level programming
D. Object-Oriented programming
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
23. Name the flowchart symbol below.
A. Terminal
B. On-page Connector
C. Off-page Connector
D. Subroutine
97. 91
77 EXAMPLES & EXERCISES OF C PROGRAMMING
24. Which of the following descriptions of the algorithm is FALSE?
A. Algorithm must specify a list of steps to be executed in the right order.
B. Algorithm can be represented using a flowchart or pseudocode.
C. Algorithm can be described as a procedure to solve a problem.
D. Algorithm is written only after the coding has been completed.
25. The following pseudocode is an example of a ____________ structure.
A. Selection
B. Nested
C. Sequential
D. Looping
26. Does the following flowchart represent which control structure in C programming?
A. if else selection structure
B. switch case selection structure
C. while repetition structure
D. do while repetition structure
27. Which of the following benefits of flowcharting is NOT one?
A. Easier coding
B. Facilitates debugging
C. Effective analysis
D. Improper documentation
START
1. Get Num
2. While(Num>0)
2.1 Sum = Sum + Num
2.2 Num = Num + 1
1. Display Sum
STOP
98. 92
77 EXAMPLES & EXERCISES OF C PROGRAMMING
28. _______________ is a compact and informal text-based approach to documenting an
algorithm.
A. Histogram
B. Pseudocode
C. Flowchart
D. IPO Chart
29. In a flowchart, the geometrical figure shown below represents.
A. Input and output
B. Subroutine
C. Decisions
D. Process
30. The following pseudocode is an example of a ____________ structure.
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
32. Which of the following is an advantage of pseudocode?
A. Difficult for beginners to follow the program logic.
B. Facilitates analysis, coding, and debugging processes.
C. No standardized style or format.
D. It does not provide a visual representation of an algorithm.
START
1. Read A, B
2. Calculate Sum = A + B
3. Display Sum
STOP
99. 93
77 EXAMPLES & EXERCISES OF C PROGRAMMING
33. Which of the following is NOT a fundamental C data type?
A. float
B. real
C. int
D. char
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
35. Identify the INCORRECT statement for 'identifier'.
A. An identifier cannot begin with a digit.
B. There cannot be any spaces between the characters in an identifier.
C. Other than underscore, no special symbols may be used.
D. Reserved words can be used as identification.
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.
39. Which one function is a need for all C programmes?
A. system ()
B. program ()
C. main ()
D. start ()
40. What type of punctuation marks the start and end of a code block?
A. { }
B. ( )
C. [ ]
D. BEGIN and END
100. 94
77 EXAMPLES & EXERCISES OF C PROGRAMMING
41. Every statement in C programming must end with a _________________.
A. period (.)
B. backslash ()
C. colon (:)
D. semicolon (;)
42. Text enclosed in /* */ in a C program ____________________.
A. gives instructions to the processor
B. declares memory requirements.
C. is ignored by the C compiler.
D. causes a syntax error.
43. Which of the following are INVALID identifiers?
i. R3D3
ii. per-capita
iii. phone#
iv. ice_cream
v. 91_gold
A. i, ii, iv, v
B. i, iv
C. ii, iii, v
D. ii, iv, v
44. What is the correct statement to declare a constant?
A. constant a=10;
B. const int a=10
C. const int a=10;
D. #define a 10;
45. An identifier in C ___________________________.
A. cannot contain any uppercase or lowercase letters, decimal digits, or the underscore
character
B. can have no more than one embedded blank
C. can begin with an underscore
D. can be a reserved word
46. Pick one of the following symbols that can be included in a variable name.
A. asterisk (*)
B. hashtag (#)
C. addition (+)
D. underscore (_)
47. Select the operator with the lowest level of precedence.
A. +
B. =
C. *
D. <
101. 95
77 EXAMPLES & EXERCISES OF C PROGRAMMING
48. Which of the following identifiers is INVALID?
i. integer
ii. printf
iii. Net-total
iv. _sum
A. i, ii and iii
B. i, iii, and iv
C. ii and iii
D. i, ii, iii, iv
49. Which of the following is a selection statement?
A. switch case
B. while
C. do while
D. for
50. Does the following flowchart represent which control structure in C?
A. if else selection structure
B. switch case selection structure
C. while repetition structure
D. for repetition structure
51. Which of the following statements best describes 'Pseudocode'?
A. A graphical depiction of data, information, and workflow that details problem-
solving steps by connecting specific symbols to flow lines.
B. Defined as a numbered list of instructions to perform some task usually used to
outline the general steps in an algorithm without having to write actual code.
C. Before the actual programme development, a list of measures to address problems
is established to ensure that the right problem-solving technique is used.
D. It lists the inputs, the outputs, and the procedures that must be taken to convert the
inputs into the outputs of a programme.
102. 96
77 EXAMPLES & EXERCISES OF C PROGRAMMING
52. Which of the statements below, best describes a ‘Flow Chart’?
A. A graphical representation of data, information, and workflow using certain symbols
that are connected to flow lines to describe the instructions carried out in problem
solving.
B. It is simply a numbered list of instructions to perform some task, usually used to
outline the general steps in an algorithm, without having to write actual code.
C. A list of measures to solve problems is created before the actual program code to
make sure the correct problem solution method is used.
D. Identify the program’s inputs, its outputs, and the processing steps that required
transforming the inputs into outputs.
53. X is best described as a / an …………………………
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?
A. printf (“ %f %d”, a, b);
B. printf (“ %f %x”, a, b);
C. printf (“ %f %s”, a, b);
D. printf (“ %f %c”, a, b);
X is a memory location in a computer’s memory to store varied value data.
To indicate the memory location, each X should be given a unique name.
X is the symbol that represents a memory location.
#include <stdio.h>
void main ()
{
float a=3.14;
unsigned char b=’&’;
}
103. 97
77 EXAMPLES & EXERCISES OF C PROGRAMMING
55. What is the output of the programme based on the flowchart below?
Start
Insert grade =50
If intGrade >= 70
Message to print:
“Passing grade”
Stop
Message to print:
“Failing grade”
No
Yes
A. Message to print: “Failing grade.”
B. Message to print: “Passing grade.”
C. Failing grade.
D. Passing grade.
56. In the documentation of a programming project, a programmer needs to determine the
functions, variables, and formulas that he should use. This statement refers to:
A. Determining requirement specification process
B. Writing project proposal, flowchart, and project plan process
C. Making specification test process
D. Writing source code process
57. Compiler generates ______________ file.
A. Executable code
B. Object code
C. Assembly code
D. None of the above
58. How many different outcomes can be achieved with a single if-else statement?
A. 1
B. 2
C. 3
D. 4
104. 98
77 EXAMPLES & EXERCISES OF C PROGRAMMING
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
61. An example of a C programme would be?
A. Variables
B. Statements
C. Functions
D. All of the above
62. Name the flowchart symbol below.
A. Terminal
B. On-page connector
C. Subroutine
D. Off-page connector
105. 99
77 EXAMPLES & EXERCISES OF C PROGRAMMING
63. What would be the output for the following C code?
A. It will print nothing
B. 0
C. -2
D. Compile error
64. Which control structure does the following flowchart represents in C?
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
#include<stdio.h>
int main()
{
int a=2;
if (a==2)
{
a=-a+2<1;
printf(“%d”,a);
}
else
{
break;
}
}
106. 100
77 EXAMPLES & EXERCISES OF C PROGRAMMING
66. What does the following code produce?
A. One
B. Zero
C. Hello World
D. ZeroHello World
67. Which of the following is NOT a looping statement?
A. for
B. do while
C. while
D. repeat until
68. The following flowchart represents which control structure in C?
A. if else selection structure
B. switch case selection structure
C. while repetition structure
D. do while repetition structure
69. What is the meaning of the code block while (x>100)?
A. While x is less than one hundred
B. While x is greater than one hundred
C. While x is equal to one hundred
D. While is wishes
int x=0;
switch(x)
{
case 1: printf(“One”);break;
case 0: printf(“Zero”); break;
case 2: printf(“Hello World”);break;
}
107. 101
77 EXAMPLES & EXERCISES OF C PROGRAMMING
70. Imprudent use of goto statement leads to?
A. Unstructured spaghetti code
B. Infinite loop
C. Break statement
D. None of the above
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?
A. 0 1 2 3 4 5 6 7 8 9
B. 0 1 2 3 4 5 6 7 8
C. 1 2 3 4 5 6 7 8 9
D. 1 2 3 4 5 6 7 8 9
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
#include<stdio.h>
void main()
{
int i;
for(i=0;i<10;i++)
printf("%d ",i);
}
108. 102
77 EXAMPLES & EXERCISES OF C PROGRAMMING
74. In the code below, how many times is the value of x tested?
A. 2
B. 4
C. 3
D. 1
75. Choose the value of NUM so that the output would be 6:
A. 2
B. 4
C. 6
D. 8
76. What does function mean?
A. A function is a collection of statements that carry out a particular activity.
B. Function is the fundamental modular unit. Typically, a function is created to
carry out a specified duty.
C. Function is a block of code that performs a specific task. It is reusable and
has a name.
D. All of the above.
77. Any C program
A. Needs input data
B. Need not contain any function
C. Must contain at least one function
D. None of the above
#include <stdio.h>
int main()
{
int x = 0;
while (x<3)
{
x++;
printf("DEC20012 Is Fun!n");
}
}
#include<stdio.h>
int main()
{
int i;
for(i=6;i>4;i-=2)
{
printf("%d", i);
}
}}
109. 103
77 EXAMPLES & EXERCISES OF C PROGRAMMING
Answer.
1. C
2. A
3. C
4. B
5. D
6. A
7. A
8. D
9. B
10. B
11. D
12. A
13. C
14. C
15. C
16. B
17. A
18. D
19. C
20. D
21. C
22. B
23. C
24. D
25. D
26. C
27. D
28. B
29. C
30. B
31. C
32. B
33. B
34. B
35. D
36. B
37. C
38. B
39. C
40. A
41. D
42. C
43. C
44. C
45. C
46. D
47. B
48. C
49. A
50. D
51. B
52. A
53. C
54. D
55. C
56. B
57. B
58. B
59. A
60. D
61. D
62. B
63. D
64. A
65. B
66. B
67. D
68. C
69. A
70. A
71. D
72. A
73. A
74. B
75. C
76. D
77. C
110. Page | vi
77 EXAMPLES & EXERCISES OF C PROGRAMMING
1. Gookin, D. (2013). Beginning Programming with C for Dummies. John Wiley & Sons.
2. Hooi, Y. K. (2018). Fundamentals of Programming Polytechnic Series. Oxford Fajar
Sdn. Bhd.
3. Kanetkar, Y. (2016). Let us C (15th
Edition). BPB publications.
4. Klemens, B. (2015). 21st Century C (Second Edition). O'Reilly Published.
5. Kochan, S. G. (2013). Programming in objective-C. Pearson Education India.
6. Learn-c.org. (n.d.). learn-c.org free interactive C tutorial. Retrieved from learn-c.org:
https://www.learn-c.org/
7. Programiz. (n.d.). Parewa Labs Pvt. Ltd. Retrieved from Learn C Programming:
https://www.programiz.com/c-programming
8. Allain, A. (2019). Cprogramming.com. Retrieved from Learn C and C++
Programming: https://www.cprogramming.com/