Computer Chapter 2 Notes
Computer Chapter 2 Notes
Computer Chapter 2 Notes
GROWING TOGETHER
EXERCISE SOLUTIONS
Q1 Multiple Choice Questions:
1. A software that facilitates programmers in writing computer programs is:
(a) a compiler (b) an editor (c) an IDE (d) a debugger
2. ___________ is a software that is responsible for the conservation of program files to the machine understandable and
executable code.
(a) Compiler (b) editor (c) IDE (d) Debugger
3. Every programming language has some primitive building blocks and follows some grammar rules known as its:
(a) Auto words (b) syntax (c) building blocks (d) semantic rules
4. A list of words that are predefined and must not be used by the programmer to his own variables is known as ________.
(a) Auto words (b) reserved words (c) restricted words (d) predefined words
5. Include statements are written in _________ section.
(a) Header (b) main (c) comments (d) print
6. ________ are added in the source code to further explain the techniques and algorithms used by the programmer.
(a) Messages (b) hints (c) comments (d) explanations
7. ________ are the values that do not change during the whole execution of program.
(a) Variables (b) Constants (c) strings (d) comments
8. A float uses bytes of memory.
(a) 3 (b) 4 (c) 5 (d) 6
9. For initializing variable, we use ___________ operator.
(a) . (b) = (c) @ (d) ?
10. ________ can be thought of as a container to store constants.
(a) Box (b) jar (c) variable (d) collection
Q2 True or False
1. An IDE combines text editors, libraries, compilers and debuggers in a single interface. F
2. Computers require the conversion of the code written in program file on the machine language in order to execute it. T
3. Column is a reserved word in C programming language. F
4. *Comment goes here* is a valid comment. F
5. float can store a real number up to six digits of precision. T
Q3 Define the following:
1) IDE: A software thar provides a programming environment to facilitate programmers in writing and executing computer
programs is as an Integrated Development Environment (IDE).
Example: Xcode Visual Studio Code::Blocks Dev C++
2) Compiler: A compiler is a software that is responsible for conversion of computer program written in some high-level
programming language to machine language.
3) Reserved Words: Every programming language has a list of words that are predefined. Each word has its specific
meaning already known to the compiler. These words are known as reserved words or keywords
Example: int, if and return are reserved words in c language.
4) Main section of a program: It consist of main () function. Every C program must contain a main () function and it is the
starting point of execution.
5) Char data type: A variable of type char store one character only. To declare character type variable in C, we use the
keyword char. It takes up just 1 byte of memory for storage.
Example: char c = ‘a’
Q4 Briefly answer the following questions.
1. Why do we need a programming environment?
Ans. In order to correctly perform any task, we need to have proper tools. For example, for gardening we need gardening
tools and for painting we need a collection of paints, brushes and canvas. Similarly, we need for programming we need proper
tools for writing program. It is essential to setup a programming environment before we start writing programs. It works as a
basic platform for us to write and execute programs.
2. Write the steps to create a C program file in the IDE of your lab computer.
Ans. Step 1. Open Code::Blocks
Step 2. Click on the file and then click new
Step 3. Write C program
Step 4. Save file with extension .c
Step 5. Click on the build and run button to see the program’s output
23. Write a program that displays your name in first line and your location in second line.
Ans. Program: #include < stdio.h >
void main () {
Output: My name is Ali.
printf(“My name is Ali. \n”); I live in Lahore
printf(“I live in Lahore.”);
}
24. Can we print new line without \n?
Ans. in the absence of an escape sequence, even if we have multiple printf statements, their output is displayed on a single
line.
Example: #include < stdio.h >
void main () {
printf(“My name is”); Output: My name is Luqman Ahmad
printf(“Luqman Ahmad”);
}
25. Define the purpose \t.
Ans. Escape sequence \t specifies I\O function of moving to the next tab stop horizontally. A tab stop is collection of 8 spaces.
Using \t takes cursor to the next tab stop. This escape sequence is used when user presents data with more spaces.
Example: #include < stdio.h >
void main (){
printf(“Name: \tLuqman\nFname: \tM.Nawaz\nMarks: \t1012”);
}
Output: Name: Luqman
26. List some basic operators used in C. Fname: M.Nawaz
Ans. There are following operators in C language: Marks: 1012
Assignment Arithmetic Logical Relational
27. Define assignment operator?
Ans. Assignment operator is used to assign a value to a variable, or assign a value of variable to another variable.
Equal sign (=) is used as assignment operator in C. Example: int sum = 5;
28. Define arithmetic operator?
Ans. arithmetic operators are used to perform arithmetic operations on data. Operator: +, -, *, /
29. Define division operator.
Ans. division operator (/) divides a value of left operand by the value of right operand. Example: float result = 3.0 / 2.0
After this statement, the variable result contains the value 1.5
30. What is a difference between 3.0/2 and 3/2.
Ans. If both the operands are of type int, then result of division is also of type int.
float 3 / 2; As both values are of type int so answer is also an integer which is 1.
float 3.0 / 2; In this case the value stored in variable result is 1.5
31. Define multiplication operator?
Ans. Multiplication operator (*) is a binary operator which performs the product of two numbers.
Example: int multiply = 5 * 5;
32. Define addition operator.
Ans. Addition operator (+) calculates the sum of two operands. Example: int add = 10 + 10;
33. Define increment and decrement operator.
Ans. The increment and decrement operators are unary operators. The increment operator ++increase the value of variable by
1. Similarly, the decrement operator -- decrease the value of variable by 1.
Example: the statement a = a + 1; is used to increase the value of a variable a by 1. In C language this statement can
also be written as a++; or ++a; .Similarly, a--; or --a; is used to decrease the value of a by 1.
34. Define subtraction operator?
Ans. subtraction operator (-) subtracts right operand from the left operand. Example: int result = 20 – 15;
35. Write a program that takes monthly income and monthly expenses and calculate monthly savings.
Ans. Program: #include < stdio.h >
void main () {
int income, expenses, savings;
printf(“Enter Monthly Income: ”);
scanf(“%d”, &income); Output: Enter Monthly Income: 50000
printf(“Enter Monthly Expenses: ”); Enter Monthly Expenses: 33000
scanf(“%d”, &expenses); Monthly savings: 17000
savings = income – expenses;
printf(“Enter Monthly Income: ”);
}