(IX) Ch-15 Variables, Constant and Data Types

1. Fill in the blanks: (a) A variable name must have 1 to 40 characters. (b) A variable name must start with an alphabet. (c) Keywords of QBASIC are not allowed to use as a variable name. (d) A variable can be declared implicitly and explicitly. (e) Explicit declaration of variables is done with DIM statements. (f) A symbolic constant is a symbol or a variable name, which represents only one value throughout the program. (g) Data types of QBASIC are : numeric and String. (h) Different numeric data types of QBASIC are sting, integer, single precision and double precision. 2. State true or false : (a) Symbol "%" is used for integer variable. True (b) A keyword can be used as a variable name. False (c) One variable can store only one value at a time. True (d) The string cannot be used in arithmetic calculations. True (e) It is good practice to declare variables at the beginning of a program. True (f) The legal range for integer data type is from -32,767 to 32,768. False (h) 'DIM N AS INTEGER' is an example of explicit declaration of variable. True 3. Write whether the given variables are valid or invalid , give reasons, if invalid:
Variables Reasons
1N$ Invalid, because start with number.
N$ Valid, because start with the alphabet.
N%1 Invalid, because of type declaration in middle.
LET Invalid, because LET is a keyword.
FIRST NAME Invalid, because of its blank space.
FNAME Valid, because it is a meaningful variable name.
4. Find out whether the following assignments are valid or invalid. Give reasons if invalid:
Variables Reasons
N$=KATHMANDU Invalid, because string data is not enclosed in double quotation mark.
N= Rs. 2000 Invalid, because numeric data only consist digit.
%N=2000 Invalid, because start with type declaration.
N1=2,000 Invalid, because number with a comma.
SAL= "2000" Invalid, because numeric variable with a string data.
N$= "Nepal" Valid, because string data is enclosed in double quotation mark.
1N=200 Invalid, because variable name start with a digit.
5. Answer the following questions: (a) Define variables and constants with examples. Answer: Variable: An identifier or reference or name for the memory address that holds data and can change its value during the execution of a program is called variable. Example of variable: Countryname$ , Age etc. Constant: Those values, which are remained fixed during the execution of a program is called constant. Example of constant: Conuntryname$ = "NEPAL" and Age = 15 etc. (b) Write any three rules of writing a variable name. Answer: 3 rules for variable naming are as follows: - A variable name must start with an alphabet ( A to Z or a to z). - A variable name may have 1 to 40 characters in length. - Reserved word or keyword of QBASIC are not allowed to use as a variable name. (c) What are the different types of data supported by QBASIC? Explain in short with their declaration symbols and memory consumption. Answer:
Data Type Declaration Symbol Data Range Memory Consumption
String $ eg. N$ , N1$ 0 to 32,767 Characters 4 bytes for descriptor, 1 byte for each character in string
Integer %
eg. N% , N1%
-32,768 to 32,767 2 bytes
Long Integer &
eg. N&, N1&
-2,147,483,648 to 2,147,483,647 4 bytes
Single Precision !
eg. N! , N1!
Accuracy up to 7 digits 4 bytes
Double Precision #
eg. N# , N1#
Accuracy up to 15 digits 8 bytes
(d) What is a string variable? Write statements to declare string variable implicitly and explicitly. Answer: A string variable is a name or reference or memory location, which stores alphanumerix characters. Implicit declaration of stiring Variable Example: LET N$ = "NEPAL" Explicit declaration of string ariable Example: DIM First.Name As STRING (e) What is variable declaration? Declare a string type of variable with an example. Answer: The process of declaring a variable name, its type and size is called variable declaration. There are two types of variable declaration. Implicit declaration of stiring Variable Example: LET N$ = "NEPAL" Explicit declaration of string ariable Example: DIM First.Name As STRING (f) Why is explicit declaration of variables better than implicit declaration? Answer: Explicit declaration of a variable is better than implicit declaration of it because it reduces the repetition of variable name. (g) What is string constant? Give an example. Answer: A text value that consists of alphabet, digits, symbols, and other special characters is called string constant. For example : "Rosebud School", " Happy Dashain 2080" etc. 6. Write down the programs for the following: (a) To store three different numbers under three different variables then find their sum and average. CLS INPUT " Enter three different numbers" , A, B, C SUM = A+B+C AVG = SUM / 3 PRINT "Sum of three different numbers is " , SUM PRINT " Average of three different numbers is ", AVG END (b)To store name, address and telephone number of a person. Print them on the screen. CLS INPUT “Enter name, address and telephone number” , N$, A$, TEL$ PRINT “NAME” , “ADDRESS” , “ TELEPHONE” PRINT N$, A$, TEL$ END (c) To find out the sum of cube of two different numbers CLS INPUT “ Enter any two different numbers” , A, B SUM = A ^ 3 + B ^ 3 PRINT “ Sum of cube of two different numbers is” , SUM END (d) To find the volume of a cylinder where Vol = PI * r2*h. (Use PI = 3.14 as symbolic constant) CLS INPUT “ Enter the radius and height of cylinder” , R , H CONST PI = 3.14 VOL = PI * R ^ 2 * H PRINT “ Volume of Cylinder is ”, VOL END (e) To store two different numbers; then find sum, difference and product of the numbers. CLS INPUT “Enter any two different numbers ” , A , B SUM = A + B DIFF = A - B PROD = A * B PRINT “SUM” , “DIFFERENCE” , “PRODUCT” PRINT SUM , DIFF , PROD END (f) To store three different strings using three variables and print the string after concatenation CLS INPUT "Enter three string : ";A$,B$,C$ ANS$=A$+B$+C$ PRINT " Three string after concatenation : ";ANS$ END (g) To store a number in a numeric variables and to calculate 10%,20%, and 25% of number and print them CLS INPUT "Enter three string : ";N A = (N * 10) / 100 B = (N * 20) / 100 C = (N * 25)/100 PRINT " 10% of number = ";A PRINT "20% of number=";B PRINT "25% of number=";C END