Uploaded by Mohd At-Tarmizi Abu Hassan

02 Ebook 77 Examples and Exercises of C Programming

advertisement
Author
Ts. Mohd At-Tarmizi Bin Abu Hassan
Publisher
Page | i
77 EXAMPLES & EXERCISES OF C PROGRAMMING
77 Examples & Exercises of C Programming
© 2022 Politeknik Sultan Idris Shah
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.
The views or opinions expressed in this release do not necessarily reflect
official policy, and the publishers and authors of this publication are not responsible for
any losses incurred by parties that act or refrain from acting as a result of information in this
publication.
Perpustakaan Negara Malaysia
Cataloguing-in-Publication Data
Mohd. At-Tarmizi Abu Hassan, Ts., 198577 EXAMPLES & EXERCISES OF C PROGRAMMING /
Author Ts. Mohd At-Tarmizi Bin Abu Hassan.
Mode of access: Internet
eISBN 978-967-2860-38-9
1. C (Computer program language).
2. Programming languages (Electronic computers).
3. Government publications--Malaysia.
4. Electronic books.
I. Title.
005.133
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.
Ts. Mohd At-Tarmizi Bin Abu Hassan
Page | iii
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!”
Page | iv
77 EXAMPLES & EXERCISES OF C PROGRAMMING
Acknowledgements
iii
Preface
iv
Table of Contents
v
77 Examples of C Programming - Sequences Structure
1-16
77 Examples of C Programming - Selection Structure
17-35
77 Examples of C Programming - Repetition Structure
36-49
77 Examples of C Programming - Array
50-60
77 Examples of C Programming - Function
61-77
77 Examples of C Programming - Pointer
78-79
77 Examples of C Programming - Applications
80-86
77 Exercices of C Programming
References and Further Studies
87-103
vi
Page | v
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
1
77 EXAMPLES & EXERCISES OF C PROGRAMMING
3 - ComputerGenie_getch.c source code
3 - ComputerGenie_getch.c output file
4 - ComputerGenie_getchar.c source code
4 - ComputerGenie_getchar.c ouput file
2
77 EXAMPLES & EXERCISES OF C PROGRAMMING
5 - ComputerGenie_getche.c source code
5 - ComputerGenie_getche.c output file
3
77 EXAMPLES & EXERCISES OF C PROGRAMMING
6 - Understand_Various_Of_Data_Types.c source code
6 - Understand_Various_Of_Data_Types.c output file
4
77 EXAMPLES & EXERCISES OF C PROGRAMMING
7 - Draw_Guns_in_the_specified_format.c source code
7 - Draw_Guns_in_the_specified_format.c output file
5
77 EXAMPLES & EXERCISES OF C PROGRAMMING
8 - Basic_Calculator.c source code
8 - Basic_Calculator.c output file
6
77 EXAMPLES & EXERCISES OF C PROGRAMMING
9 - Puts_and_gets_name_age.c source code
9 - Puts_and_gets_name_age.c output file
7
77 EXAMPLES & EXERCISES OF C PROGRAMMING
10 - Understanding_Precedence.c source code
10 - Understanding_Precedence.c output file
8
77 EXAMPLES & EXERCISES OF C PROGRAMMING
11 - Undertanding_Relational_Operator.c source code
11 - Undertanding_Relational_Operator.c output file
9
77 EXAMPLES & EXERCISES OF C PROGRAMMING
12 - Compound_Assignment.c source code
12 - Compound_Assignment.c output file
10
77 EXAMPLES & EXERCISES OF C PROGRAMMING
13 - AverageTemperature.c source code
13 - AverageTemperature.c output file
11
77 EXAMPLES & EXERCISES OF C PROGRAMMING
14 - CalculateCircleCircumferenceandArea.c source code
14 - CalculateCircleCircumferenceandArea.c output file
12
77 EXAMPLES & EXERCISES OF C PROGRAMMING
15 - CalculateCubicAreaAndVolume.c source code
15 - CalculateCubicAreaAndVolume.c output file
16 - CalculateMonthlySaving.c source code
16 - CalculateMonthlySaving.c output file
13
77 EXAMPLES & EXERCISES OF C PROGRAMMING
17 - ConvertDistance.c source code
17 - ConvertDistance.c output file
18 - ConvertTemperatureCentigradetoFahrenheit.c source code
18 - ConvertTemperatureCentigradetoFahrenheit.c output file
14
77 EXAMPLES & EXERCISES OF C PROGRAMMING
19 - ConvertTemperatureFahrenheittoCentigrade.c source code
19 - ConvertTemperatureFahrenheittoCentigrade.c output file
20 - Base_and_Power.c source code
20 - Base_and_Power.c output file
15
77 EXAMPLES & EXERCISES OF C PROGRAMMING
21 - Square_Root.c source code
21 - Square_Root.c output file
22 - Bigger_or_Smaller_or_Equal.c source code
22 - Bigger_or_Smaller_or_Equal.c output file
16
77 EXAMPLES & EXERCISES OF C PROGRAMMING
2. SELECTION STRUCTURE
23 - If under age.c source code
23 - If under age.c output file
17
77 EXAMPLES & EXERCISES OF C PROGRAMMING
24 - AgeForVoting.c source code
24 - AgeForVoting.c output file
A. If statement output file
B. Else statement output file
18
77 EXAMPLES & EXERCISES OF C PROGRAMMING
25 - Find_Largest_Number.c source code
25 - Find_Largest_Number.c output file
19
77 EXAMPLES & EXERCISES OF C PROGRAMMING
26 - Find_Smallest_Number.c source code
26 - Find_Smallest_Number.c output file
20
77 EXAMPLES & EXERCISES OF C PROGRAMMING
27 - Odd_or_Even_Number.c source code
27 - Odd_or_Even_Number.c output file
21
77 EXAMPLES & EXERCISES OF C PROGRAMMING
28 - Switch_Case_Fruits_Integer.c source code
28 - Switch_Case_Fruits_Integer.c output file
22
77 EXAMPLES & EXERCISES OF C PROGRAMMING
29 - Switch_Case_Blood_Type.c source code
29 - Switch_Case_Blood_Type.c output file
23
77 EXAMPLES & EXERCISES OF C PROGRAMMING
30 – Nested_If_Voted.c source code
30 – Nested_If_Voted.c output file
24
77 EXAMPLES & EXERCISES OF C PROGRAMMING
31 - Gred_Examination_System.c source code
31 - Gred_Examination_System.c output file
25
77 EXAMPLES & EXERCISES OF C PROGRAMMING
32 – Blood_Pressure.c source code
32 – Blood_Pressure.c output file
26
77 EXAMPLES & EXERCISES OF C PROGRAMMING
33 - Body_Mass_Index.c source code
27
77 EXAMPLES & EXERCISES OF C PROGRAMMING
33 - Body_Mass_Index.c output file
28
77 EXAMPLES & EXERCISES OF C PROGRAMMING
34 - Fever_Check.c source code
34 - Fever_Check.c output file
29
77 EXAMPLES & EXERCISES OF C PROGRAMMING
35 - Calculate_Electricity_Bill.c source code
35 - Calculate_Electricity_Bill.c output file
30
77 EXAMPLES & EXERCISES OF C PROGRAMMING
36 - Calculate_Water_Bill.c source code
31
77 EXAMPLES & EXERCISES OF C PROGRAMMING
36 - Calculate_Water_Bill.c output file
32
77 EXAMPLES & EXERCISES OF C PROGRAMMING
37 - Calculate_Resistor_Value.c source code
33
77 EXAMPLES & EXERCISES OF C PROGRAMMING
34
77 EXAMPLES & EXERCISES OF C PROGRAMMING
37 - Calculate_Resistor_Value.c output file
35
77 EXAMPLES & EXERCISES OF C PROGRAMMING
3. REPETITION STRUCTURE
38 - While_Statement_CountDown_In_Ascending_Order.c source code
38 - While_Statement_CountDown_In_Ascending_Order.c output file
36
77 EXAMPLES & EXERCISES OF C PROGRAMMING
39 - While_Statement_CountDown_In_Descending_Order.c source code
39 - While_Statement_CountDown_In_Descending_Order.c output file
37
77 EXAMPLES & EXERCISES OF C PROGRAMMING
40 - Do_While_Statement_CountDown_In_Ascending_Order.c source code
40 - Do_While_Statement_CountDown_In_Ascending_Order.c ouput file
38
77 EXAMPLES & EXERCISES OF C PROGRAMMING
41 - Do_While_Statement_CountDown_In_Ascending_Order.c source code
41 - Do_While_Statement_CountDown_In_Ascending_Order.c output file
39
77 EXAMPLES & EXERCISES OF C PROGRAMMING
42 - For_Statement_CountDown_In_Ascending_Order.c source code
42 - For_Statement_CountDown_In_Ascending_Order.c output file
40
77 EXAMPLES & EXERCISES OF C PROGRAMMING
43 - For_Statement_CountDown_In_Descending_Order.c source code
43 - For_Statement_CountDown_In_Descending_Order.c output file
41
77 EXAMPLES & EXERCISES OF C PROGRAMMING
44 - Pre_Test_While_Statement.c source code
44 - Pre_Test_While_Statement.c output file
42
77 EXAMPLES & EXERCISES OF C PROGRAMMING
45 - Post_Test_Do_While_Statement.c source code
45 - Post_Test_Do_While_Statement.c output file
43
77 EXAMPLES & EXERCISES OF C PROGRAMMING
46 - Average_Marks_For_Statement.c source code
46 - Average_Marks_For_Statement.c output file
44
77 EXAMPLES & EXERCISES OF C PROGRAMMING
47 - Reverse_Number.c source code
47 - Reverse_Number.c utput file
45
77 EXAMPLES & EXERCISES OF C PROGRAMMING
48 - Countdown_In_Descending_Order.c source code
48 - Countdown_In_Descending_Order.c output file
46
77 EXAMPLES & EXERCISES OF C PROGRAMMING
49 - Electrical_Power_Calculator.c source code
47
77 EXAMPLES & EXERCISES OF C PROGRAMMING
48
77 EXAMPLES & EXERCISES OF C PROGRAMMING
49 - Electrical_Power_Calculator.c output file
49
77 EXAMPLES & EXERCISES OF C PROGRAMMING
4. ARRAY
50 - 1Array_Initializing.c source code
50 - 1Array_Initializing.c output file
50
77 EXAMPLES & EXERCISES OF C PROGRAMMING
51 - 2Array_with_Loop.c source code
51 - 2Array_with_Loop.c output file
51
77 EXAMPLES & EXERCISES OF C PROGRAMMING
52 - 3Array_Two_Dimentional.c source code
52 - 3Array_Two_Dimentional.c output file
52
77 EXAMPLES & EXERCISES OF C PROGRAMMING
53 - 4Passing_Arrays_as_Function_Arguments.c source code
53 - 4Passing_Arrays_as_Function_Arguments.c output file
53
77 EXAMPLES & EXERCISES OF C PROGRAMMING
54 - 5Array_Find_Average.c source code
54 - 5Array_Find_Average.c output file
54
77 EXAMPLES & EXERCISES OF C PROGRAMMING
55 - 6Array_Find_Total.c source code
55 - 6Array_Find_Total.c output file
55
77 EXAMPLES & EXERCISES OF C PROGRAMMING
56 - 7Array_Total_Daily_Rainfall_For_A_Weeks.c source code
56 - 7Array_Total_Daily_Rainfall_For_A_Weeks.c output file
56
77 EXAMPLES & EXERCISES OF C PROGRAMMING
57 - 8Array_Average_Marks.c source code
57 - 8Array_Average_Marks.c output file
57
77 EXAMPLES & EXERCISES OF C PROGRAMMING
58 - 9Array_Find_Event_Number.c source code
58
77 EXAMPLES & EXERCISES OF C PROGRAMMING
58 - 9Array_Find_Event_Number.c output file
59
77 EXAMPLES & EXERCISES OF C PROGRAMMING
59 - 10Array_Find_Average_For_Two_Dimentional_Marks.c source code
59 - 10Array_Find_Average_For_Two_Dimentional_Marks.c output file
60
77 EXAMPLES & EXERCISES OF C PROGRAMMING
5. FUNCTION.
60 - 1Function_User_Defined.c source code
61
77 EXAMPLES & EXERCISES OF C PROGRAMMING
60 - 1Function_User_Defined.c output file
61 - 2Function_Find_Minimum_Value.c source code
61 - 2Function_Find_Minimum_Value.c output file
62
77 EXAMPLES & EXERCISES OF C PROGRAMMING
62 - 3Function_Call_By_Value.c source code
62 - 3Function_Call_By_Value.c output file
63
77 EXAMPLES & EXERCISES OF C PROGRAMMING
63 - 4Function_Call_By_Reference.c source code
63 - 4Function_Call_By_Reference.c output file
64
77 EXAMPLES & EXERCISES OF C PROGRAMMING
64 - 5Function_Annual_Income.c source code
64 - 5Function_Annual_Income.c output file
65
77 EXAMPLES & EXERCISES OF C PROGRAMMING
65 - 6Function_Calculate Volume For Sphere.c source code
65 - 6Function_Calculate Volume For Sphere.c output file
66
77 EXAMPLES & EXERCISES OF C PROGRAMMING
66 - 7Function_Draw_Face.c source code
67
77 EXAMPLES & EXERCISES OF C PROGRAMMING
66 - 7Function_Draw_Face.c output file
68
77 EXAMPLES & EXERCISES OF C PROGRAMMING
67 - 8Function_Choose_Face_To_Draw.c source code
69
77 EXAMPLES & EXERCISES OF C PROGRAMMING
70
77 EXAMPLES & EXERCISES OF C PROGRAMMING
71
77 EXAMPLES & EXERCISES OF C PROGRAMMING
67 - 8Function_Choose_Face_To_Draw.c output file
72
77 EXAMPLES & EXERCISES OF C PROGRAMMING
68 - 9Function_Draw_House.c source code
73
77 EXAMPLES & EXERCISES OF C PROGRAMMING
68 - 9Function_Draw_House.c output file
74
77 EXAMPLES & EXERCISES OF C PROGRAMMING
69 - 10Function_Pass_by_Value.c source code
69 - 10Function_Pass_by_Value.c output file
75
77 EXAMPLES & EXERCISES OF C PROGRAMMING
70 - 11Function_Sum1.c source code
70 - 11Function_Sum1.c output file
76
77 EXAMPLES & EXERCISES OF C PROGRAMMING
71 - 12Function_Sum2.c source code
71 - 12Function_Sum2.c output file
77
77 EXAMPLES & EXERCISES OF C PROGRAMMING
6. POINTER.
72 - Pointer.c source code
72 - Pointer.c output file
73 - Pointer_AccessThevalueofavariables.c source code
73 - Pointer_AccessThevalueofavariables.c output file
78
77 EXAMPLES & EXERCISES OF C PROGRAMMING
74 - Pointer_ReferenceOperatorandDereferenceOperator.c source code
74 - Pointer_ReferenceOperatorandDereferenceOperator.c output file
79
77 EXAMPLES & EXERCISES OF C PROGRAMMING
7. APPLICATIONS.
75 - Calculate_Paracetamol_Dose.c source code
75 - Calculate_Paracetamol_Dose.c output file
80
77 EXAMPLES & EXERCISES OF C PROGRAMMING
76 - Games_Tic_Tac_Toe.c source code
81
77 EXAMPLES & EXERCISES OF C PROGRAMMING
82
77 EXAMPLES & EXERCISES OF C PROGRAMMING
76 - Games_Tic_Tac_Toe.c output file
83
77 EXAMPLES & EXERCISES OF C PROGRAMMING
77 - Ohms Law Calculator.c source code
84
77 EXAMPLES & EXERCISES OF C PROGRAMMING
85
77 EXAMPLES & EXERCISES OF C PROGRAMMING
77 - Ohms Law Calculator.c output file
86
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:
#include<stdio.h>
int main()
{
int i=1;
i= 2+2*i++;
printf(“%d”,i);
return 0;
}
A.
B.
C.
D.
3
4
5
6
5. In a "switch-case" statement, which of the following cannot be checked?
A.
enum
B.
integer
C.
character
D.
float
87
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?
#include<stdio.h>
int main ()
{
int i=0;
for (i=1; i<=5; i++)
{printf ("%d", i);}
return 0;
}
A.
B.
C.
D.
012345
01234
1234
12345
9. Identify the elements in planning process.
i.
Calculation
ii.
Process
iii.
Input
iv.
Output
v.
Progress
A.
B.
C.
D.
i, ii, and iii only
ii, iii and iv only
iii and iv only
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
88
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.
89
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.
B.
C.
D.
Terminal
On-page Connector
Off-page Connector
Subroutine
90
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.
START
1. Get Num
2. While(Num>0)
2.1 Sum = Sum + Num
2.2 Num = Num + 1
1. Display Sum
STOP
A.
B.
C.
D.
Selection
Nested
Sequential
Looping
26. Does the following flowchart represent which control structure in C programming?
A.
B.
C.
D.
if else selection structure
switch case selection structure
while repetition structure
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
91
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.
B.
C.
D.
Input and output
Subroutine
Decisions
Process
30. The following pseudocode is an example of a ____________ structure.
START
1. Read A, B
2. Calculate Sum = A + B
3. Display Sum
STOP
A.
B.
C.
D.
Selection
Sequential
Repetition
Nested
31. Identify which control structure does the following flowchart represent in C programming?
A.
B.
C.
D.
if selection structure
switch case selection structure
if else selection structure
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.
92
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
93
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.
B.
C.
D.
i, ii, iv, v
i, iv
ii, iii, v
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.
<
94
77 EXAMPLES & EXERCISES OF C PROGRAMMING
48. Which of the following identifiers is INVALID?
i. integer
ii. printf
iii. Net-total
iv. _sum
A.
B.
C.
D.
i, ii and iii
i, iii, and iv
ii and iii
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.
B.
C.
D.
if else selection structure
switch case selection structure
while repetition structure
for repetition structure
51. Which of the following statements best describes 'Pseudocode'?
A.
A graphical depiction of data, information, and workflow that details problemsolving 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.
95
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 …………………………
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.
A.
B.
C.
D.
Operator
Constant
Variable
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=’&’;
}
A.
B.
C.
D.
printf (“ %f %d”, a, b);
printf (“ %f %x”, a, b);
printf (“ %f %s”, a, b);
printf (“ %f %c”, a, b);
96
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
No
Message to print:
“Failing grade”
Yes
Message to print:
“Passing grade”
Stop
A.
B.
C.
D.
Message to print: “Failing grade.”
Message to print: “Passing grade.”
Failing grade.
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
97
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.
B.
C.
D.
i and iii
i and iv
ii and iii
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.
B.
C.
D.
Terminal
On-page connector
Subroutine
Off-page connector
98
77 EXAMPLES & EXERCISES OF C PROGRAMMING
63. What would be the output for the following C code?
#include<stdio.h>
int main()
{
int a=2;
if (a==2)
{
a=-a+2<1;
printf(“%d”,a);
}
else
{
break;
}
}
A.
B.
C.
D.
It will print nothing
0
-2
Compile error
64. Which control structure does the following flowchart represents in C?
A.
B.
C.
D.
if
if else
nested if else
switch case
65. What must be done to prevent failing from one case to the next?
A.
B.
C.
D.
stop;
break;
end;
A semicolon
99
77 EXAMPLES & EXERCISES OF C PROGRAMMING
66. What does the following code produce?
int x=0;
switch(x)
{
case 1: printf(“One”);break;
case 0: printf(“Zero”); break;
case 2: printf(“Hello World”);break;
}
A.
B.
C.
D.
One
Zero
Hello World
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.
B.
C.
D.
if else selection structure
switch case selection structure
while repetition structure
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
100
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?
#include<stdio.h>
void main()
{
int i;
for(i=0;i<10;i++)
printf("%d ",i);
}
A.
B.
C.
D.
0123456789
012345678
123456789
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.
B.
C.
D.
i and iii
i and iv
ii and iii
ii and iv
101
77 EXAMPLES & EXERCISES OF C PROGRAMMING
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.
B.
C.
D.
2
4
3
1
75. Choose the value of NUM so that the output would be 6:
#include<stdio.h>
int main()
{
int i;
for(i=6;i>4;i-=2)
{
printf("%d", i);
}
}}
A.
B.
C.
D.
2
4
6
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
102
77 EXAMPLES & EXERCISES OF C PROGRAMMING
Answer.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
C
A
C
B
D
A
A
D
B
B
D
A
C
C
C
B
A
D
C
D
C
B
C
D
D
C
D
B
C
B
C
B
B
B
D
B
C
B
C
A
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
D
C
C
C
C
D
B
C
A
D
B
A
C
D
C
B
B
B
A
D
D
B
D
A
B
B
D
C
A
A
D
A
A
B
C
D
C
103
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/
Page | vi
Download