Ch. No.10 Data Types, Assignment and Input / Output Statements Q.10.01 Complete the following statements. i) BASIC stands for________________________________________________ ii) Basic is well suited for computers having ________________ memory. iii) In BASIC, key F1 is equivalent to the command ________________ iv) ________________ are the part of program which run in compiler’s mode. v) To run a program press ________________ key. vi) ________________ Statements allows you to enter value of variable while the program is running. Ans: i) Beginner’s All-purpose Symbolic Instruction Code ii) limited iii) LIST iv) Statement v) F2 vi) Input Q.10.02 Which of the following statements is True or False? i) Commands but not statements can be entered at any time. ii) If a filename already exist, then SAVE will over write it. iii) A string constant cannot contain quotation marks. iv) REM statement is executable statement v) A computer prints strings without adding spaces when strings are separated by semicolon in print statement. vi) Print sends output to a printer.

vii) Input is an executable statement. viii) Data statement may be placed anywhere in a program, Ans: i) False vi) False

ii) True vii) False

iii) False viii) True

iv) False

v) True

Q.10.03 Encircle one Choice A, B, C or D I each case i) Which of the following word can be used as a variable name? (A) List (B) Load (C) REM (D) Pencil ii) Which of the following commands is used to exit from BASIC and to return to DOS? (A) CLEAR (B) KILL (C) SYSTEM (D) NEW iii) The maximum characters that a string constant may contain are: (A) 8 (B) 32 (C) 80 (D) 255 iv) Which of the following is not a logical operator. 1

(A) AND (B) OR (C) FIX (D) NOT v) Which of the following types of the characters can be used as numeric variable name? (A) special (B) blank spaces (C) hyphen (D) none vi) Equality operator is a: (A) Logical operator (B) Functional operator (C) Relational operator (C) Arithmetical operator Ans: i) (D)

ii) (C)

iii) (D)

iv) (C)

v) (D)

vi) (C)

Q.10.04 Match the items given in Column I with those given in Column II i) ii) iii) iv) v) vi)

Column I Functional operator Save Command Load Logical Operator List

Column II a) F1 b) AND c) F2 d) RND e) F4 f) Less Than g) Print h) F3

Ans: i) (d)

ii) (e)

iii) (g)

iv) (h)

v) (b)

vi) (a)

Q.10.05 Write a short note on BASIC and describe some of its important features. Ans: BASIC stands for Beginner’s All-purpose Symbolic Instruction Code. It is an easy highlevel computer programming language. It was developed in 1964. BASIC quickly became one of the most popular programming languages. Basic is first high level programming language to be used on microcomputers. First interpreter of BASIC language was written in 1975 by Microsoft. Basic has the following features: i) It is an easy, student’s friendly language. ii) It is a general purpose language, suitable for scientific and commercial programming. iii) It has a simple syntax rules and is easy to test and debug. iv) Communication with computer is simple. v) It is well suited for computers having limited memory. 2

Q.10.06 What types of entries are made in BASIC? What is difference between commands and statements? Ans: There are three types of entries in BASIC. These are Commands, Statements and data. Commands and Statements can be entered at any time. Data can be entered with the data and assignment statement or when asked by the computer Commands: Commands are generally executed in direct mode. Commands usually perform some type of program maintenance such as editing, clearing, running or saving. Statements: Statements are the part of the computer program and run in compiler’s mode or in indirect mode. Statements are executed during running the program.

Q.10.07 Describe briefly the following commands. Auto, Delete, List, Renum and System Ans: AUTO: Purpose: It generates an automatic line number when each time ENTER key is pressed. Syntax: Following is the syntax of Auto Command. Auto When line number and increment are not specified, then both assume the value 10. On pressing ENTER key each time a new line number is displayed as 10, 20, 20, and so on.

DELETE Purpose: It deletes Program lines or line range. Syntax: Following is the syntax of Delete Command. Delete 50 It deletes the specified the line number. LIST Purpose: It displays the program or part of the program currently in the memory. Syntax: Following is the syntax of List Command. List It lists all program and lines currently in memory. RENUM Purpose: To renumber program lines without changing the order of the program. Syntax: Following is the syntax of Renum Command. Renum 100, 35, 10 First number specifies the new number in the new sequence for the first line. Second number specifies old number of the line in 3

the current program. Third number specifies increment to be used in the new sequence. SYSTEM Purpose: It is used to exit BASIC by entering the system command. Save program before Pressing Enter Key. Syntax: Following is the syntax of System Command. System Press ENTER key Q.10.08 How many types of variables names are used in BASIC? Give examples of invalid variable names with explanation. Ans: Ans: There are two types of variables: (i) (ii)

Numeric variable String variable

(i)

Numeric variable: A numeric variable always has a numeric value either supplied by the user or calculated by the computer during the program execution. A numeric variable name may consist of all alphabets and numeric constants but must be began with an alphabet Special character and blank spaces are not allowed in numeric variable name. Examples are A, B2, Num etc.

(ii)

String Variable: A string variable represents a character string that is a character or a sequence of characters. A string variable name must be followed by a dollar sign ($) such as A$, B33$ GRAD$ etc. Blank spaces may be included in a string but not the quotation marks.

Invalid variable names with explanation Examples of invalid variable names are: Invalid Variable Name Explanation 5NUM cannot begin with a digit TOT PAY Black Space are not allowed NET-PAY Hyphen not allowed B&W Special symbol (&) not allowed LIST cannot use reserve word

Q.10.09 Ans:

valid Variable Name NUM5 TOTPAY NETPAY BW

Describe various types of constants used in BASIC with examples.

(i) (ii)

There are two types of constant: Numeric Constants String Constants

(i)

Numeric Constants: 4

(ii)

Numeric constants are positive or negative numbers. A numeric constant in BASIC cannot contain comma. Examples of numeric constants are 23, -46, 5.6, -7.4 String Constants: A sting constant is any set of (maximum 255) character enclosed in quotation marks. For Example “I LOVE PAKISTAN”, “47+17=?”

Q.10.10 Pick out numeric and strings from the following data items: “Lahore” ; 27 ; “60W” ; 1.008 ; “16” ; 20km ; “23RD MARCH” ; IX ; A-1 Ans: Numeric Data: 27 , 1.008 , 20km , IX, A-1 String Data: “Lahore” , “60W” , “16” , “23RD MARCH” Q.10.11 Explain various types of operators used in BASIC with examples. Ans: There are four types of operators are used in GW Basic. (i) Arithmetic Operator (ii) Relational Operator (iii) Logical Operator (iv) Functional Operator (i) Arithmetic Operators: Numeric expressions contain operators that perform arithmetic operation such as addition, subtraction, multiplication etc. These are called arithmetic operators. Following are arithmetic operators in GW Basic. Operation Addition Subtraction

Operator + -

Multiplication Division Power Negation

* / ^ -

(ii) Relational Operators: Relational operators are used to relate or compare two quantities. The result of comparison may be either YES or NO or may be TRUE or FALSE. When these operators appear in a computer program, their results decide the flow of the computer program. Following are Relational Operators in GW Basic. Operator = < > =

Relation Equality Inequality Less than Greater than Less than or equal to Greater than or equal to 5

(iii) Logical Operators: Logical Operators such as AND, OR, NOT, NAND, NOR and XOR perform in the same way as in Boolean algebra. The result of these logical operations would be either true or false. In a computer program, such results are very important because they decide the flow of the program. (iv) Functional Operators: BASIC is very rich in having predetermined functions that perform arithmetic and string operations. These are built-in functions such as ABS, INT, FIX, RND, SQR, LOG, SIN and COS. These functions make the programming much simpler.

Q.10.12 Ans:

How does computer know whether an instruction you typed is a program. When we type instructions in indirect mode then computer know these instructions are program. Program must have line numbers.

Q.10.13 Ans:

What commands clears the computer’s memory? NEW command clears the computer’s memory.

Q.10.14 Ans:

What will be the output? When you enter PRINT “37 C =” 37*9/5+32 Output will be 37 C = 98.6

Q.10.15 Write a note on assignment statement with examples. Ans: ASSIGNMENT STATEMENTS The assignment statement is used when the programmer wishes to give a variable an initial

value that will be used throughout the program, or when a variable is assigned a new value as a result of computation or operation. The assignment statement has the following format: Line # (Let) variable name = variable or expression The word let is optional in BASIC assignment. The term expression is defined as a combination of two or more variables. It can be one or more constants and operators Let us write a program using assignment statement. 10 P = 25 20 Q = 45 30 Print P*Q 40 End Q.10.16 Ans:

What is difference between STOP and END statement? Explain it with example. STOP: STOP command pause the current running statement. With the help of CONT command we can run program again. END: 6

END statement usually typed at the end of the program. It indicates program is finished. Q.10.17 Ans:

Q.10.18 Ans:

Q.10.19

Write a program that prints three numbers, their sum and average using LET statement. 10 LET A = 10 20 LET B = 15 30 LET C = 20 40 LET SUM = A+B+C 50 LET AVG = SUM/3 60 PRINT “NUMBERS =” ; A;B;C 70 PRINT “SUM =” ; SUM 80 PRINT “AVERAGE =” ; AVG 90 END Write a program that prints three numbers, their sum and average using INPUT statement. 10 INPUT “ENTER ANY NUMBER”;A 20 INPUT “ENTER ANY NUMBER”;B 30 INPUT “ENTER ANY NUMBER”;C 40 LET SUM = A+B+C 50 LET AVG = SUM/3 60 PRINT “NUMBERS =” ; A;B;C 70 PRINT “SUM =” ; SUM 80 PRINT “AVERAGE =” ; AVG 90 END

Write a program for the square and cube of any number using INPUT statement. Print the number, its square and cube in Zones.

Ans: 10 INPUT “ENTER ANY NUMBER”;A 20 LET SQUARE = A ^ 2 30 LET CUBE = A ^ 3 40 PRINT “SQUARE OF NO.” ;A; “=” ; SQUARE 50 PRINT “CUBE OF NO.” ;A; “=” ; CUBE 60 END OUTPUT ENTER ANY NUMBER ? 16 SQUARE OF NO. 16 = 256 CUBE OF NO. 16 = 4096 Q.10.20

Tell whether the following are valid BASIC statement/program. If not, find a possible correction and its output if any. 7

Ans:

Ans:

Ans:

Ans:

Ans:

a) 10 INPUT “ENTER A NUMBER”;N This program is valid. b) 10 INPUT “ NUMBERS =” X,Y,Z This program is not valid. Correction: 10 INPUT “NUMBER =”; X,Y,Z c) 10 INPUT A$ + 2 This program is not valid. Correction: 10 INPUT A$ d) 10 READ A; B 20 PRINT 6,9 30 PRINT A+B This program in not valid, Correction: 10 READ A, B 20 PRINT A,B 30 DATA 6,9 e) 10 READ A$, B$; C$ 20 PRINT “SUBJECTS” 30 DATA ENGLISH, PHYSICS, MATHEMATICS 40 END This program in not valid,

Correction: 10 READ A$, B$, C$ 20 PRINT “SUBJECTS” ; A$, B$, C$ 30 DATA ENGLISH, PHYSICS, MATHEMATICS 40 END f)

Ans:

10 READ A,B,C 20 LET X=(A+B)*C 30 PRINT X 40 END This program in not valid, Correction: 10 READ A,B,C 20 LET X=(A+B)*C 30 PRINT X 40 DATA 10,20,30

8

g) 10 INPUT A,B$ 20 C=A+B$ 30 PRINT A : PRINT B$ : PRINT C 40 END Ans: This program in not valid, Correction: 10 INPUT A,B 20 C=A+B 30 PRINT A : PRINT B : PRINT C 40 END Q.10.21What will be the output of the following program? 10 READ A$ 20 PRINT A$ 30 RESTORE 40 READ B$ 50 PRINT B$ 60 RESTORE 70 READ C$ 80 PRINT C$ 90 DATA ASLAM, AKRAM, AFZAL 100 END Q.10.22 Ans:

Write a BASIC program to calculate the sum, product and average of 4 numbers using INPUT statement and READ – DATA statement. Program using INPUT statement:

10 CLS 20 INPUT “ENTER ANY NUMBER”;A 30 INPUT “ENTER ANY NUMBER”;B 40 INPUT “ENTER ANY NUMBER”;C 50 INPUT “ENTER ANY NUMBER”;D 60 SUM =A+B+C+D 70 PROD = A*B*C*D 80 AVG = SUM/4 90 PRINT SUM 100 PRINT PROD 110 PRINT AVG 120 END

Program using READ and DATA statement: 10 CLS 20 READ A, B, C, D 9

30 SUM =A+B+C+D 40 PROD = A*B*C*D 50 AVG = SUM/4 60 PRINT SUM 70 PRINT PROD 80 PRINT AVG 90 DATA 45,26,34,53 100 END

10