77 Examples and Exercises of C Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 111
At a glance
Powered by AI
The document discusses a book about C programming examples and exercises. It covers various programming concepts and is intended to help students learn and practice C programming.

The book is dedicated to students learning the subject of fundamental programming and is intended to enhance understanding and skills in C programming through various examples and exercises.

The book covers C programming concepts like sequences structure, selection structure, repetition structure, arrays, functions, pointers and applications.

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., 1985-
77 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 87-103

References and Further Studies vi

Page | v
77 EXAMPLES & EXERCISES OF C PROGRAMMING 1

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


77 EXAMPLES & EXERCISES OF C PROGRAMMING 2

3 - ComputerGenie_getch.c source code

3 - ComputerGenie_getch.c output file

4 - ComputerGenie_getchar.c source code

4 - ComputerGenie_getchar.c ouput file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 3

5 - ComputerGenie_getche.c source code

5 - ComputerGenie_getche.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 4

6 - Understand_Various_Of_Data_Types.c source code

6 - Understand_Various_Of_Data_Types.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 5

7 - Draw_Guns_in_the_specified_format.c source code

7 - Draw_Guns_in_the_specified_format.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 6

8 - Basic_Calculator.c source code

8 - Basic_Calculator.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 7

9 - Puts_and_gets_name_age.c source code

9 - Puts_and_gets_name_age.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 8

10 - Understanding_Precedence.c source code

10 - Understanding_Precedence.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 9

11 - Undertanding_Relational_Operator.c source code

11 - Undertanding_Relational_Operator.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 10

12 - Compound_Assignment.c source code

12 - Compound_Assignment.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 11

13 - AverageTemperature.c source code

13 - AverageTemperature.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 12

14 - CalculateCircleCircumferenceandArea.c source code

14 - CalculateCircleCircumferenceandArea.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 13

15 - CalculateCubicAreaAndVolume.c source code

15 - CalculateCubicAreaAndVolume.c output file

16 - CalculateMonthlySaving.c source code

16 - CalculateMonthlySaving.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 14

17 - ConvertDistance.c source code

17 - ConvertDistance.c output file

18 - ConvertTemperatureCentigradetoFahrenheit.c source code

18 - ConvertTemperatureCentigradetoFahrenheit.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 15

19 - ConvertTemperatureFahrenheittoCentigrade.c source code

19 - ConvertTemperatureFahrenheittoCentigrade.c output file

20 - Base_and_Power.c source code

20 - Base_and_Power.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 16

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


77 EXAMPLES & EXERCISES OF C PROGRAMMING 17

2. SELECTION STRUCTURE
23 - If under age.c source code

23 - If under age.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 18

24 - AgeForVoting.c source code

24 - AgeForVoting.c output file


A. If statement output file

B. Else statement output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 19

25 - Find_Largest_Number.c source code

25 - Find_Largest_Number.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 20

26 - Find_Smallest_Number.c source code

26 - Find_Smallest_Number.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 21

27 - Odd_or_Even_Number.c source code

27 - Odd_or_Even_Number.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 22

28 - Switch_Case_Fruits_Integer.c source code

28 - Switch_Case_Fruits_Integer.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 23

29 - Switch_Case_Blood_Type.c source code

29 - Switch_Case_Blood_Type.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 24

30 – Nested_If_Voted.c source code

30 – Nested_If_Voted.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 25

31 - Gred_Examination_System.c source code

31 - Gred_Examination_System.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 26

32 – Blood_Pressure.c source code

32 – Blood_Pressure.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 27

33 - Body_Mass_Index.c source code


77 EXAMPLES & EXERCISES OF C PROGRAMMING 28

33 - Body_Mass_Index.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 29

34 - Fever_Check.c source code

34 - Fever_Check.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 30

35 - Calculate_Electricity_Bill.c source code

35 - Calculate_Electricity_Bill.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 31

36 - Calculate_Water_Bill.c source code


77 EXAMPLES & EXERCISES OF C PROGRAMMING 32

36 - Calculate_Water_Bill.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 33

37 - Calculate_Resistor_Value.c source code


77 EXAMPLES & EXERCISES OF C PROGRAMMING 34
77 EXAMPLES & EXERCISES OF C PROGRAMMING 35

37 - Calculate_Resistor_Value.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 36

3. REPETITION STRUCTURE

38 - While_Statement_CountDown_In_Ascending_Order.c source code

38 - While_Statement_CountDown_In_Ascending_Order.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 37

39 - While_Statement_CountDown_In_Descending_Order.c source code

39 - While_Statement_CountDown_In_Descending_Order.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 38

40 - Do_While_Statement_CountDown_In_Ascending_Order.c source code

40 - Do_While_Statement_CountDown_In_Ascending_Order.c ouput file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 39

41 - Do_While_Statement_CountDown_In_Ascending_Order.c source code

41 - Do_While_Statement_CountDown_In_Ascending_Order.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 40

42 - For_Statement_CountDown_In_Ascending_Order.c source code

42 - For_Statement_CountDown_In_Ascending_Order.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 41

43 - For_Statement_CountDown_In_Descending_Order.c source code

43 - For_Statement_CountDown_In_Descending_Order.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 42

44 - Pre_Test_While_Statement.c source code

44 - Pre_Test_While_Statement.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 43

45 - Post_Test_Do_While_Statement.c source code

45 - Post_Test_Do_While_Statement.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 44

46 - Average_Marks_For_Statement.c source code

46 - Average_Marks_For_Statement.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 45

47 - Reverse_Number.c source code

47 - Reverse_Number.c utput file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 46

48 - Countdown_In_Descending_Order.c source code

48 - Countdown_In_Descending_Order.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 47

49 - Electrical_Power_Calculator.c source code


77 EXAMPLES & EXERCISES OF C PROGRAMMING 48
77 EXAMPLES & EXERCISES OF C PROGRAMMING 49

49 - Electrical_Power_Calculator.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 50

4. ARRAY
50 - 1Array_Initializing.c source code

50 - 1Array_Initializing.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 51

51 - 2Array_with_Loop.c source code

51 - 2Array_with_Loop.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 52

52 - 3Array_Two_Dimentional.c source code

52 - 3Array_Two_Dimentional.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 53

53 - 4Passing_Arrays_as_Function_Arguments.c source code

53 - 4Passing_Arrays_as_Function_Arguments.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 54

54 - 5Array_Find_Average.c source code

54 - 5Array_Find_Average.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 55

55 - 6Array_Find_Total.c source code

55 - 6Array_Find_Total.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 56

56 - 7Array_Total_Daily_Rainfall_For_A_Weeks.c source code

56 - 7Array_Total_Daily_Rainfall_For_A_Weeks.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 57

57 - 8Array_Average_Marks.c source code

57 - 8Array_Average_Marks.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 58

58 - 9Array_Find_Event_Number.c source code


77 EXAMPLES & EXERCISES OF C PROGRAMMING 59

58 - 9Array_Find_Event_Number.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 60

59 - 10Array_Find_Average_For_Two_Dimentional_Marks.c source code

59 - 10Array_Find_Average_For_Two_Dimentional_Marks.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 61

5. FUNCTION.
60 - 1Function_User_Defined.c source code
77 EXAMPLES & EXERCISES OF C PROGRAMMING 62

60 - 1Function_User_Defined.c output file

61 - 2Function_Find_Minimum_Value.c source code

61 - 2Function_Find_Minimum_Value.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 63

62 - 3Function_Call_By_Value.c source code

62 - 3Function_Call_By_Value.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 64

63 - 4Function_Call_By_Reference.c source code

63 - 4Function_Call_By_Reference.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 65

64 - 5Function_Annual_Income.c source code

64 - 5Function_Annual_Income.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 66

65 - 6Function_Calculate Volume For Sphere.c source code

65 - 6Function_Calculate Volume For Sphere.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 67

66 - 7Function_Draw_Face.c source code


77 EXAMPLES & EXERCISES OF C PROGRAMMING 68

66 - 7Function_Draw_Face.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 69

67 - 8Function_Choose_Face_To_Draw.c source code


77 EXAMPLES & EXERCISES OF C PROGRAMMING 70
77 EXAMPLES & EXERCISES OF C PROGRAMMING 71
77 EXAMPLES & EXERCISES OF C PROGRAMMING 72

67 - 8Function_Choose_Face_To_Draw.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 73

68 - 9Function_Draw_House.c source code


77 EXAMPLES & EXERCISES OF C PROGRAMMING 74

68 - 9Function_Draw_House.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 75

69 - 10Function_Pass_by_Value.c source code

69 - 10Function_Pass_by_Value.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 76

70 - 11Function_Sum1.c source code

70 - 11Function_Sum1.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 77

71 - 12Function_Sum2.c source code

71 - 12Function_Sum2.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 78

6. POINTER.
72 - Pointer.c source code

72 - Pointer.c output file

73 - Pointer_AccessThevalueofavariables.c source code

73 - Pointer_AccessThevalueofavariables.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 79

74 - Pointer_ReferenceOperatorandDereferenceOperator.c source code

74 - Pointer_ReferenceOperatorandDereferenceOperator.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 80

7. APPLICATIONS.

75 - Calculate_Paracetamol_Dose.c source code

75 - Calculate_Paracetamol_Dose.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 81

76 - Games_Tic_Tac_Toe.c source code


77 EXAMPLES & EXERCISES OF C PROGRAMMING 82
77 EXAMPLES & EXERCISES OF C PROGRAMMING 83

76 - Games_Tic_Tac_Toe.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 84

77 - Ohms Law Calculator.c source code


77 EXAMPLES & EXERCISES OF C PROGRAMMING 85
77 EXAMPLES & EXERCISES OF C PROGRAMMING 86

77 - Ohms Law Calculator.c output file


77 EXAMPLES & EXERCISES OF C PROGRAMMING 87

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. 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
77 EXAMPLES & EXERCISES OF C PROGRAMMING 88

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. 012345
B. 01234
C. 1234
D. 12345

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
77 EXAMPLES & EXERCISES OF C PROGRAMMING 89

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.
77 EXAMPLES & EXERCISES OF C PROGRAMMING 90

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
77 EXAMPLES & EXERCISES OF C PROGRAMMING 91

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. 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
77 EXAMPLES & EXERCISES OF C PROGRAMMING 92

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.

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

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.
77 EXAMPLES & EXERCISES OF C PROGRAMMING 93

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
77 EXAMPLES & EXERCISES OF C PROGRAMMING 94

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. <
77 EXAMPLES & EXERCISES OF C PROGRAMMING 95

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.
77 EXAMPLES & EXERCISES OF C PROGRAMMING 96

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. 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=’&’;
}

A. printf (“ %f %d”, a, b);


B. printf (“ %f %x”, a, b);
C. printf (“ %f %s”, a, b);
D. printf (“ %f %c”, a, b);
77 EXAMPLES & EXERCISES OF C PROGRAMMING 97

55. What is the output of the programme based on the flowchart below?

Start

Insert grade =50

Message to print:
If intGrade >= 70 No
“Failing grade”

Yes

Message to print:
“Passing grade”

Stop

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

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
77 EXAMPLES & EXERCISES OF C PROGRAMMING 99

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. 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
77 EXAMPLES & EXERCISES OF C PROGRAMMING 100

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. 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
77 EXAMPLES & EXERCISES OF C PROGRAMMING 101

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. 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

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. 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
77 EXAMPLES & EXERCISES OF C PROGRAMMING 103

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.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy