You are on page 1of 20

Unit # 5 Exercise Questions

Computer Programming

class: 8th

Q4. Give brief answers to the following questions. i) What is program? Give few examples of programs. Ans: A set of instructions given to the computer to perform a specific task is called a program. A computer works according to the instruction written in program. Few examples of programs are: A program to find area of a circle for a gives radius. A program to find sum and average of five values A program to find the grades of students A program to find greater of two values ii) What is the purpose of programming language? Ans: A programming language is designed to develop programs or instructions to communicate with computer to solve various problems. Programming languages are the means of communication between user and computer. iii) Differentiate between a constant and variable. Ans: A quantity whose value cannot be changed during execution of program is called constant. A quantity whose value can be changed during execution of program is called variable. Variables are the named memory locations that can store data and result of processing. Variables give meaningful names to constants. iv) Differentiate between Syntax error and logical error. Ans: Syntax error is a type of error that occurs when a program statement does not follow the syntax of programming language. A type of error that occurs due to wrong logic of the program is called logical error.

v) What are the rules for defining/declaring variables in GW-BASIC? Ans: Following are the rules for defining/declaring variables in GW-BASIC: Alphabets and numbers can be used for variables. The first character of the variable should be an alphabet. No special symbol is allowed except Underscore ( _ ). Underscore ( _ ) cannot be used as a first or last character. vi) Name different types of constants with examples. Ans: Constants are classified as string constants and numeric constants. String constants: These are series of alphabetic or alphanumeric character written in double quotation marks. For example Pakistan , Haider and H.No.107 et c. Numeric constants: These are the numbers, for example 15 , 20.50 , -50 etc. vii) Give the precedence of the arithmetic operators. Ans: Precedence is the priority that is followed while applying operators in an expression. In other words the order in which operators in an expression are evaluated is known as precedence. It is also known as hierarchy of operators. The precedence of arithmetic operators is given below: Priority level Operator Symbol Precedence First Exponential Operator

^ Highest Second Multiplication, Divide *OR /

Third MOD Operator MOD

Fourth Addition or Subtraction + OR Lowest

viii) What is the purpose of modulus operator (MOD)? Ans: Modulus operator is used to get the remainder after division. For example if a = 10 and b = 3 , the MOD operator ( a MOD b) will return 1 after performing division. ix) What is the purpose of following GW-BASIC commands?

1. LIST

b. RUN

c. LOAD

d. SAVE

Ans: a) LIST Command: List command is used to display all or specific statements of a program from memory on screen. The shortcut key for list command is F1. Syntax: LIST [line number][- line number] Example: LIST it displays al lines of the program. b) RUN Command: RUN command is used to execute the program that is currently loaded in the memory. The shortcut key for RUN command is F2. Syntax: RUN Example: RUN it runs the program currently in memory. c) LOAD Command: LOAD command is used to load a saved program file from disk to memory. The shortcut key for list command is F3. Syntax: LOAD filename Example: LOAD Test.bas it loads the file Test.bas in memory.

d) SAVE Command: SAVE command is used store a program from memory to disk permanently. The shortcut key for list command is F4. Syntax: SAVE filename Example: SAVE Test.bas it stores the file Test.bas from memory to disk.

Q5. (i) What is arithmetic expression? Explain different types of operators with examples. Ans: Arithmetic Expression: An expression that consists of different arithmetic operators, variables and constants is called arithmetic expression. It is evaluated by performing arithmetic operation on numeric values. There are three common types of operators.

Arithmetic Operators ( +, -, *, /, ^, MOD) Assignment Operator (=) Relational Operators (< , >, , =) a) Arithmetic Operators: These are use to perform mathematical calculations like addition,

subtraction, division, multiplication and exponentiation. Their explanation with examples is as follows: Operator symbol Operation Description Example + Addition Gives the sum of values A+B+C, X+50, 67+45 Subtraction Gives the difference of values X-56, A-C, 80-56 * Multiplication

Gives the product of values A*B, Y*50, 45*60 / Division Gives the quotient X/Y, B/40, 125/25 ^ Exponentiation Raise the value to the power of an exponent A ^ 10, 5^ 2, A^B MOD MODULUS Gives the remainder after division 35 MOD 6, X MOD Y

b)

Assignment Operator: An operator which is used to assign a value to a variable is called

assignment operator. In programming languages Equal to sign (=) is used as an assignment operator. For example A=5 , X = A + 2.54 etc.

c)

Relational Operators: These are used to perform comparisons on two values. The result

of comparison is either true (non zero) or false (zero). Following is their description with examples: Operator symbol Operation Description Example = Equal to Returns true if the values are equal and false if not A= B Not equal to Returns true if the values are not equal and false if they are equal. 57 > Greater than Returns true if the first number is greater than second and false if not 15 > 11 Less than Returns true if the first number is less than second and false if not

7<9 >= Greater than OR equal to Returns true if the first number is greater than or equal to the second and false if not (X+1) >= 7 Less than OR equal to Returns true if the first number is less than or equal to the second and false if not Y

(v) Explain the purpose of the following GW-BASIC statements with their syntax and examples. a. PRINT b. INPUT c. READ and DATA d. IF-THEN-ELSE

Ans: a) PRINT Statement: PRINT statement is used to display message or output to the screen. As shortcut? can be used for PRINT command. Syntax: line number PRINT [string/variables/constants] String: It indicates the sequence of characters that is displayed on screen. Variables: It indicates the variables whose values to be displayed on screen. Constants: It indicates the constants to be displayed on screen. For example: 10 PRINT I Love Pakistan 10 PRINT X, Y, Z will print I Love Pakistan will print values of variables x, Y and Z

10 PRINT 205 , -101

will print constant values 205 and -101

b) INPUT Statement: INPUT statement is used to take input from the user during the execution of the program. Syntax: line number PRINT [string;] list of variables

String is used to prompt the user to enter the required value using keyboard and it is optional. List of variables is used to store the entered values. Example: 10 20 30 CLS INPUT Enter a number, X PRINT Square of your number is=, X^2

This program will get value for X from user at execution time and will print its square. c) READ and DATA Statement: READ/DATA statement is a combination of two

statements used together when there is a need to process large number of variables with given data. READ statement defines the list of variables while DATA statement contains constant values for these variables in READ statement. Values in READ and DATA statements should be separated by commas. Syntax: line number READ list of variables separated by commas. line number DATA list of constants separated by commas. Example: 10 20 READ X, Y, Z, K DATA 8, 9, 13, 15

The above program will specify four variables x, y, z and k and assign them values 8, 9, 13 and 15 respectively. If values in DATA statement are more than variables in READ statement then

the extra values are ignored but if the variables in READ statement are more than the values in DATA statement then syntax error encounters i.e. Out of Data. d) IF-THEN-ELSE Statement: IF-THEN-ELSE is a decision making statement, depending

upon the condition, it takes some action or changes the order of execution. It helps the computer to check whether a condition is true or false. Syntax: IF expression/condition THEN statement(s)1 ELSE statement(s)2

If the expression or condition is true then Statement(s)1 will be executed otherwise Statement(s)2 will be executed. T Flowchart: Condition Statement (s) Statement (s) F Output: Enter your marks: 65 You have passed Example: 10 20 30 CLS INPUT Enter your marks:; marks IF marks >= 40 THEN PRINT You have passed

40

END

Short Questions other than exercise i) Explain the use of semicolon with PRINT.

Ans: Two or more variables can be separated in print statement by semicolon. If semicolon is used the second value appears right after the first value. There is no space between values. Example: PRINT Apple ; Mango ; Orange Output will be as: AppleMangoOrange ii) Explain the use of comma with PRINT.

Ans: Two or more variables can be separated in print statement by comma. If comma is used each value is displayed in 14 spaces. A maximum of five values can be printed in single line using commas. Example: PRINT Apple , Mango , Orange Output will be as: Apple iii) Mango Orange

What is expression? What are its types?

Ans: An expression is a combination of symbols (operands) and operators that represent a value. There are two types of expressions: Arithmetic expression uses arithmetic operators to perform arithmetic calculations. Relational expression uses relational operators to compare two values. iv) Define programming.

Ans: Programming is a technique to develop programs. Different programming languages are used to write programs. Simply computer programming is all about developing, implementing and maintaining the programs. v) How arithmetic expression can be assigned to a variable?

Ans: An arithmetic expression can be assigned to a variable by using assignment operator (=). The variable is written on the left side of assignment operator and expression is written on right side of assignment operator. First expression is evaluated and result is stored in variable e.g. a = c d * 10 vi) Differentiate between bug and debugging.

Ans: An error in a program is called a bug. The process of finding and removing errors in a program is called debugging. Errors can be found by testing the program. vii) Differentiate between save and load command.

Ans: SAVE command is used to store a program from memory to the disk permanently whereas load command is used to load a stored program from disk to memory. viii) How the commands are differ from statements?

Ans: Commands are key words which are used to issue instructions to the computer to perform specific task. They do not need any line number while statements are instructions for the computer program to perform an action. Each statement is started with a line number. Statements are executed according to line number in ascending order. ix) Ans: Differentiate between string constant and numeric constant. The string constant may consist of alphanumeric or numeric data and is written in double quotation marks while numeric constant consists of numeric data i.e. only numbers and is written without quotation marks. x) Why variables are used with INPUT statement?

Ans: The variables are used with INPUT statement to store the input data. Two or more variables must be separated by comma. xi) Write the use of $ in GW-BASIC.

Ans: A dollar sign $ is used after a string variable name such as A$ and Address$. The dollar sign indicates that the variable contains a string value. xii) What is the use of arithmetic operators?

Ans: Arithmetic operators are used to perform arithmetic calculations like addition, subtraction, division, multiplication and exponentiation. xiii) What is the use and syntax of CLS command?

Ans: CLS Command: CLS stands for Clear Screen. It is used to clear the contents of the screen. Syntax: xiv) Define precedence. CLS

Ans: Precedence is the priority that is followed while applying operators in an expression. In other words the order in which operators in an expression are evaluated is known as precedence. It is also known as hierarchy of operators. xv) Define the syntax of programming language.

Ans: The syntax of programming language is the set of rules that defines the combination of symbols used by that language xvi) What is Out of Data error?

Ans: If the variables in READ statement are more than the values in DATA statement then syntax error encounters i.e. Out of Data

Unit # 4 Exercise Questions Q4. Give brief answers to the following questions. i)

Problem Solving

What are the key features of a problem statement?

Ans: A problem statement should have the following features: Clarity and precision Identification of what would be studied Identification of key factors or variables Identification of key concepts and terms ii) What is flowchart?

Ans: Flowchart is a graphical representation of the solution of a problem. Standard symbols are used to represent different steps of solution. These symbols are linked together with arrows. These arrows show the flow of steps performed to solve a problem. iii) What are the uses of flowchart?

Ans: Flowchart helps in finding the solution of a problem and facilitates in showing the input, process and output of the problem. It is helpful in understanding the logic of the problem. iv) What are the advantages of drawing flowchart?

Ans: Flowchart is a helpful tool in solving a problem. Different benefits of flowchart are as follows: It helps to understand the logic of the problem solving process. It provides graphical representation of the process to find the solution of a problem. It displays the flow of process in a logical order.

v)

What is the use of decision symbol?

Ans: Diamond symbol is used to represent decision step in the flowchart. A condition is given in the diamond symbol. The flow of control from diamond symbol may go in two possible directions. It goes to one direction if the condition is true and in other if it is false. Q5. Give detailed answers to the following questions. i) Explain the elements of a problem statement.

Ans: Different elements of a problem statement are as follows: What is given - the input The processing requirements What is required the Output 1. 1. What is given - the input: Input is the raw material given to the computer to solve

a problem. It is also called data or raw facts. The problem statement defines the inputs required to solve a problem. The correct input must be given in order to get the correct output. Problem: Preparing Tea: The solution of above problem requires different input material such as electric kettle, tea bags, water, milk and sugar 1. The Processing Requirements: It includes the operations and actions performed on input to get desired results. The problem statement defines the processing requirements for the solution of a problem. Problem: Preparing Tea: The processing requirements for this problem are as follows: Boiling the water in the kettle

Adding sugar, tea bags and milk 1. 3. What is required the Output:

The problem statement defines the required output. Output is the required result that is obtained after processing. It is solution of a problem. Problem: Preparing Tea: The output of this problem is the prepared tea that is served in the cup. 1. What is flowcharting? How flowcharts help in problem solving? Ans: Flowcharting is a process of creating flowcharts for solving a problem. It is a tool to analyze different processes. It can be used in different fields such as programming, engineering and science etc. Flowchart: Flowchart is a graphical representation of the solution of a problem. Standard symbols are used to represent different steps of solution. These symbols are linked together with arrows. These arrows show the flow of steps performed to solve a problem. Benefits of Flowchart: Flowchart is a helpful tool in solving a problem. Different benefits of flowchart are as follows: It helps to understand the logic of the problem solving process. It provides graphical representation of the process to find the solution of a problem. It displays the flow of process in a logical order. 1. Explain different symbols used to draw flowcharts with examples. Ans: Flowchart Symbols The standard symbols are used to draw a flowchart. The commonly used standard symbols to draw flowchart are as follows.

1. 1.

Start/ Terminal

Oval is used to represent the start and end of the flowchart. It is also called terminal. Example: the following example displays the start and end steps in the flowchart. Start End 1. 2. Input/ Output

Parallelogram is used to represent an input or output step in the flowchart. Example: the following example displays the input and output steps in the flowchart. Input M Display M

1. 3.

Process

Rectangle is used to represent a processing or computational operation in the flowchart. Example: the following example displays the processing steps in the flowchart. Sum = a + b 4. Flow lines

Arrows are used to represent the direction of flow in the flowchart. There are four flow lines to represent four directions.

1. 5.

Decision

Diamond symbol is used to represent decision steps in the flow chart. A condition is given in the diamond. The flow of control from diamond symbol may go in two possible directions. It goes to one direction if the condition true or yes and to second direction if condition is false or no. No Yes Is n>5 ? Example: the following example displays the decision step in the flowchart. Short Questions Other than Exercise 1. Define a problem. Ans: A problem is an obstacle, hurdle, difficulty or challenge or any situation that needs to be solved. 2. Define problem solving. Ans: Problem solving is a process to solve different problems. The goal of problem solving is to get the desired result in a short and perfect manner. 3. Name the steps involved to solve problem in computer. Ans: Problem solving includes the following steps: 1. 2. 3. 4. 5. i. ii. Iii. iv. v. Identification of problem Specify requirements Analyze the problem Design algorithm & draw flowchart Write the program / Coding

6. 7. 8. 9.

vi. vii. viii. ix.

Test and debug the program Implement the program Maintain and update the program Documentation

10. Differentiate between problem and problem statement. Ans: A problem is an obstacle, hurdle, difficulty or challenge or any situation that needs to be solved, while a problem statement is concise description of the problem to be solved. 4. Write any three guidelines for flowcharting. Ans: Following guidelines should be followed while drawing flowchart: Flowchart should be clear, neat and easy to follow. Flowchart is either from left to right or top to bottom. Only one flow line should enter and come out of the process symbol. Only one flow line should enter decision symbol but two flow lines could come out of the decision symbol, one for each possible answer. 5. Draw process symbol. Why process symbol is used in flowchart? Ans: Rectangle is used to represent a processing or computational operation in the flowchart.

6. What is the purpose of parallelogram and rectangle symbol in flowchart? Ans: Parallelogram is used to represent an input or output step in the flowchart.

7. Which is the terminal symbol in flowchart? Ans: Oval is used to represent the start and end of the flowchart. It is also called terminal.

8. What is the goal of problem solving? Ans: the goal of problem solving is to get the desired result in a short and perfect manner. 9. What are the limitations of flow chart? Ans Flowchart has following limitations: It is difficult to draw a flowchart for a complex problem. If alterations are required, flowchart is to be redrawn. 10. Write the elements of problem statement. Ans: Different elements of a problem statement are as follows: What is given - the input The processing requirements What is required the Output 11. How you can make flowchart more effective? Ans: Avoid the intersection of flow lines, while drawing a flowchart, to make it more effective. 12. How flowchart symbols are connected? Ans: Lines with arrow heads are used to connect symbols and show the flow of logic and data in a flowchart.

You might also like