Kerala Plus One Computer Science Chapter Wise Questions and Answers Chapter 6 Data Types and Operators
Very Short Answer Type Questions
(Score 1)
Question 1.
Void data type uses bytes of memory.
a. 2
b. 1
c. no space
d. 4
Answer:
c. no space
Question 2.
Conditional operator is operator.
a. unary
b. binary
c. ternary
d. none of these
Answer:
c. ternary
Question 3.
To perform a ternary operation how many number of operands needed?
a. 2
b. 1
c. 4
d. 3
Answer:
d. 3
Question 4.
Which one is not a fundamental data type?
a. int
b. array
c. float
d. char
Answer:
b. array
Question 5.
The char data type is internally treated as
Answer:
integers
Question 6.
…………. data type uses 8 bytes of memory.
a. void
b. double
c. float
d. char
Answer:
b. double
Question 7.
operator used for getting remainder during division.
a. /
b.*
c. &
d. %
Answer:
d. %
Question 8.
The multiple use of input or output operators in a single statement is known as
Answer:
cascading of I/O operators
Question 9.
The participants of an operation are called
Answer:
operands
Question 10.
Arrange the fundamental data types in ascending order of size.
Answer:
Void, char, int, float, double.
Question 11.
The name given to a storage location is known as
Answer:
variable
Question 12.
Name a ternary operator in C++.
Answer:
Conditional operator (?:)
Short Answer Type Questions
(Score 2)
Question 1.
What are the guidelines used to write stylistic programs?
Answer:
- Use suitable naming convention for identifiers.
- Use clear and simple expressions.
- Use comments wherever needed.
- Relevance of indentation.
Question 2.
What do you mean by an expression?
Answer:
An expression is composed of operators and operands. All expressions can be evaluated to get a result. This result is known as the value returned by the expression.
Short Answer Type Questions
(Score 3)
Question 1.
Write the precedence of operators.
Answer:
- ( ) parentheses .
- ++, —, !, unary +, unary -, sizeof
- – *, /, %
- – +, –
- <,<=,>, > =
- == , ! =
- && (logical AND)
- 11 (logical OR)
- ?: (conditional expression)
- =, *=,/=,%=,+=, -=
- , (comma)
Question 2.
What are the points to be remembered in the choice of variable names? Explain with example.
Answer:
- Choose good mnemonic names for all variables, functions and procedures. e.g. avg_hgt, Roll_No.
- Use standardized prefixes and suffixes for related variables, e.g. num1,num2.
- Assign names to constants in the beginning of the program, e.g. float PI 1 = 3.14.
Question 3.
Write a program to find the sum ; of two integer numbers.
Answer:
#include <iostream> using namespace std; int main() { int num1, num2, sum; cout<<“Enter two numbers: cin>>num1>>num2; sum = num1 + num2; cout<<“Sum of the entered numbers = ”<<sum; return 0; }
Long Answer Type Questions
(Score 5)
Question 1.
Write points are to be noted while commenting.
Answer:
- Always insert prologues, the comments in the beginning of a program that summarises the purpose of the program.
- Comment each variable and constant declaration.
- Use comments to explain complex program steps.
- It is better to include comments while writing the program itself.
- Write short and clear comments.
Question 2.
Write a. C++ program to find the average of marks in three sub?jects.
Answer:
#include <iostream> using namespace std; int main() { int score1, score2, score3; float avg; cout << “Enter the three marks:"; cin >>score1>>score2 >> score3; avg =(score1+score2+score3)/3.o; cout << “Average Score is: ” << avg; return o; }
Question 3.
Write a program to find the area and perimeter of a circle for a given radius.
Answer:
#include <iostream> using namespace std; int main( ) { const float pi = 22.0/7; float r, A, P; cout<<“Enter the radius of the circle”; cin> >r; A = pi * r * r; P = 2 * pi * r; cout<<“Area of the circle:”<<A < <“\n”; cout<<“Perimeter of the circle =” <<P; return 0; }
Question 4.
Write a C++ program to find the simple interest.
Answer:
#include <iostream> using namespace std; int main() { float p, n, r, interest; cout<<“Enter the principal amount in Rupees: ”; cin>>p; cout<<“Enter the number of years for the deposit: ”; cin>>n; cout<<“Enter the rate of interest: ”; cin>>r; interest = p * n * r /too; cout<<“Simple interest is:”<<interest; return O; }
Question 5.
Write a program to convert temperature from Celsius to Fahren?heit.
Answer:
#include <iostream> using namespace std; int main( ) { float Celsius, fahrenheit; cout<<“Enter the Temperature in Celsius: ”; cin>>celsius; fahrenheit=i.8*celsius+32; cout<<celsius<<“Degree Celsius=”<< fahrenheit<<“Degree Fahrenheit”; return o; }
Question 6.
Program to find the ASCII value of a character.
Answer:
#include <iostream> using namespace std; int main( ) { char ch; int asc; . cout << “Enter the character:”; cin >> ch; asc = ch; cout << “ASCII value of ”<<ch<<“ =” << asc; return o; }
Textual Questions & Answers
Question 1.
What are data types? List all pre-defined data types in C++.
Answer:
The data type specifies the nature of data we have to store, the set of operations that can be performed on the data. Predefined data types in C++ are char, int, float, double and void.
Question 2.
What is a constant? (1)
Answer:
Constants are values that remain constant throughout the execution of the program. The keyword const is used to create constant in C++.
Question 3.
What is dynamic initialisation of variables? (l)
Answer:
A variable initialised during the execution of the program and is known as dynamic initialisation, eg., float p = x * y;
Question 4.
What is type casting? (2)
Answer:
Sometimes the programmer may decide the data type of the result of evaluation. This is done by the programmer by specifying the data type within parentheses to the left of the operand. Since the programmer explicitly casts a data to the desired type, it is also known as type casting.
Question 5.
Write the purpose of declaration statement? (1)
Answer:
It must be declared in the program before its use. When we declare a variable, we tell the compiler about the type of data that will be stored in it.
Question 6.
Name the header file to be included to use cin and cout in programs? (1)
Answer:
The header file iostream contains the information about the objects cin and cout.
Question 7.
What is the input operator “>>” and output operator “<<” called ? (1)
Answer:
The input operator “>>” is known as get from or extraction operator. The output operator “<<” is known as put to or insertion operator.
Question 8.
What will be the result of a = 5/3 if a is
i. float
ii. int (2)
Answer:
i. 1. because result of integer expression is integer and it is assigned to float
a.
ii. 1
Question 9.
What will be the value of P= P++ +
++i where i is 22 and P= 3 initially? (1)
Answer:
P = 27
Question 10.
Find the value given by the following expression if j =5 initially.
i. (5*++j)%6
ii. (5*j++)%6 (2)
Answer:
i. o
ii. 1
Question 11.
What will be the order of evaluation for following expressions? (2)
i. i+5>=j-6
ii. s+10<p-2+2*q
Answer:
i. First i+5 then j-6 and then >=
ii. 2*q s + 10 P-2
Question 12.
What will be the result of the following if ans is 6 initially? (2)
i. cout<<ans = 8;
ii. cout << ans == 8;
Answer:
i. 8
ii. The result will be 0 (false) because 6 is not equal to 8.
Question 13.
What is a variable? List the two values associated with it. (2)
Answer:
Variables are the names given to memory locations.
There are two values associated with a variable. They are R-value and L-value. The R-value represents the data and L-value represents the memory address of the variable.
Question 14.
In how many ways can a variable be declared in C++? (3)
Answer:
In C++, variable can be declared in three ways. They are:
- Uninitialised variable, eg., int a;
- Initialised variable, eg., int a = 2;
- Dynamically initialised variable, eg., int a = b + 10;
Question 15.
Explain the impact of type modifiers of C++ in variable declaration. (2)
Answer:
It help us to alter the size, range or precision. Modifiers precede the data type name in the variable declaration. It alters the range of values permitted to a data type by altering the memory size and sign of values. Type modifiers are signed, unsigned, long and short.
Question 16.
What is the role of the keyword ‘const’? (1)
Answer:
Constants are values that remain constant throughout the execution of the program. The keyword const is used to create constant in C++.
Question 17.
Explain how prefix form of increment operation differs from postfix form. (3)
Answer:
Increment operator is used to increment the value by 1. In the prefix form, the operator is placed before the operand and the increment operation is carried out first. So, this method is often called change, then use method. When increment operation is per-formed in postfix form, the operator is placed after the operand. So it is called use, then change method. eg., If a = 10, b = 5 then c = ++a, the value of a and c will be 11. c = a++, the value of a will be 11 and c will be 10.
Question 18.
Write down the operation performed by sizeof operator. (1)
Answer:
The operator sizeof is a unary compile time operator that returns the amount of memory space in bytes allocated for the operand.
Question 19.
Explain the two methods of type conversions. (3)
Answer:
It can be done in two ways:
- Implicit type conversion: It is per-formed by C++ compiler internally.
In expressions where different types of data are involved, C++ converts the lower sized operands to the data type of highest sized operand. Since the conversion is always from lower type to higher, it is also known as type pro-motion. - Explicit type conversion: Some-times the programmer may decide the data type of the result of evaluation. This is done by the programmer by specifying the data type within parentheses to the left of the operand. Since the programmer explicitly casts a data to the desired type, it is also known as type casting. Usually, type casting is applied on the variables in the expressions.
Question 20.
What would happen if main( ) is not present in a program? (1)
Answer:
The execution starts at main( ) and ends within main( ). If we use any other function in the program, it is called (or invoked) from main().
Question 21.
Identify the errors in the following code segments: (2)
a.
int main() { cout << “Enter two numbers” cin >> num >> auto float area = Length * breadth ; }
b.
#include <iostream> using namespace std void Main() { int a, b cin <<a <<b max=(a > b) a:b cout>max }
Answer:
a. No header file, no semicolon is used at the end of second and third statement. Variables not declared a return value not specified, auto cannot be used here.
b. M is upper case in main(), no semicolon used in any statements. Operators of cin and cout are wrong. Missing in conditional operator ?.
Question 22.
Find out the errors, if any, in the following C++ statements: (5)
1. cout<< “a=” a;
2. m=5, n=12; 015
3. cout << “x” ; <<x;
4. cin > > y
5. cout>>\n “abc”
6. a = b + c
7. break = x
Answer:
1. After “a=” an operator << is missing.
2. Data type is missing, variable name of 015 is missing.
3. First semicolon is an error.
4. Semicolon is missing at the end.
5. Semicolon is missing at the end. << should be used after cout. \n must be in double quote.
6. Semicolon is missing at the end. Data type is missing.
7. Semicolon is missing at the end. break is a keyword.
Question 23.
What is the role of relational operators? Distinguish between = = and =. (3)
Answer:
Used for comparing numeric data. These are binary operators. The result of any relational operation will be either True or False. They are < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == (equal to), != (not equal to). = is an assignment operator used to assign a value to the variable. == is a relational operator that tests for equality.
Question 24.
Comments are useful to enhance readability and understandability of a program. Justify this statement with examples. (5)
Answer:
Comments are lines in code that are added to describe the program. They are ignored by the compiler.
- Single line comment: The characters // (two slashes) is used. The text appearing after // in a line is treated as a comment by the C++ compiler.
- Multiline comments: Anything written within /* and */ is treated as comment so that the comment can take any number of lines.
eg., /* This program displays the message “Smoking is injurious to health” on the monitor */
#include <iostream.h> // To use the cout object using namespace std; // To access cout int main() //program begins here { //The following output statement displays a message cout << “Smoking is injurious to health”; return 0; } //end of the program
Question 25.
Explain the operators of C++ in detail. (5)
Answer:
Based on number of operands required for the operation, operators are classified into:
- Unary operator: It operates on a single operand. Commonly used unary operators are unary + (positive) and unary- (negative). These are used to represent the sign of a number.
- Binary operators: It operate on two operands.
- Ternary operators: Operates on three operands. Example is the conditional operator (?: ).
Based on the nature of operation, operators are classified into:
- Arithmetic operators: These are defined to perform basic arithmetic operations such as addition, subtraction, multiplication and division. The symbols used for this are +, – ,* and / respectively. It provides a special operator, % (modulus operator) for getting remainder during division.
- Relational operators: Used for comparing numeric data. These are binary operators. The result of any relational operation will be either True or False.
- Logical operators: Used to combine relational operations and it gives either true or false.
- Input/Output operators: It is used to perform input and output operation. The >> operator is known as get from or extraction operator. The operator << is used for output operation and is called put to or insertion operator.
- Assignment operators: It is used to assign the value of a right side to the left side variable. The operator = is used.
- Increment (++) / Decrement (—) operators: Increment/decrement operator is used to increment/decrement the value by 1.
- Conditional operators: This is a ternary operator applied over three operands.
- sizeof operator: The operator sizeof is a unary compile-time operator that returns the amount of memory space in bytes allocated for the operand.
Question 26.
Explain the different types of expressions in C++ and the methods of type conversions in detail. (5)
Answer:
On the basis of the operators used, expressions are mainly classified into:
1. Arithmetic expression:
It is an expression in which only arithmetic operators are used. The operands are numeric data and they may be variables or constants. The value returned by these expressions is also numeric. Arithmetic expressions are further classified into:
- Integer expression: If an arithmetic expression contains only integer operands, it is called integer expression.
- Floating point or real expression: It is ah expression that is composed of only floating point data. It returns a floating point result.
- Constant expression: If all the operands are constant values, then it is called constant expression.
2. Relational expression:
When relational operators are used in an expression, it is called relational expression and it produces Boolean type results like True (1) or False (o).
3. Logical expressions:
It combine two or more relational expressions with logical operators and produce either True or False as the result.
4. Implicit type conversion:
It is per-formed by C++ compiler internally. In expressions where different types of data are involved, C++ converts the lower sized operands to the data type of highest sized operand. Since the conversion is always from lower type to higher, it is also known as type pro-motion.
5. Explicit type conversion:
Some-times the programmer may decide the data type of the result of evaluation. This is done by the programmer by specifying the data type within parentheses to the left of the operand. Since the programmer explicitly casts a data to the desired type, it is also known as type casting. Usually, type casting is applied on the variables in the expressions.
Question 27.
Write the working of arithmetic assignment operator? Explain all arithmetic assignment operators with the help of examples. (5)
Answer:
A simple arithmetic statement can be expressed in a more condensed form using arithmetic assignment operators.
eg., a=a+10 can be represented as a+=10. Here += is an arithmetic assignment operator. The arithmetic assignment operators in C++ are +=, -=. *=> /=, %= These are also known as C++ short-hands. These are all binary operators and the first operand should be a variable. The use of these operators makes the two operations (arithmetic and assignment) faster than the usual method.
X += 10 i.e., x = x + 10
X -= 10 i.e., x = x – 10
X *= 10 i.e., x = x * 10
X /= 10 i.e., x = x / 10
X %= 10 i.e., x = x % 10
Leave a Reply