Kerala Plus One Computer Science Chapter Wise Previous Questions Chapter 6 Data Types and Operators
Question 1.
Consider the following statements in C++ (March – 2015)
if(mark>=18)
cout<<“Passed”;
else
cout<<“Failed”;
Suggest an operator in C++ using which the same output can be produced.
Answer:
Conditional operator (?:)
Question 2.
Raju wants to add value 1 to the variable ‘p’ and store the new value in ‘p’ itself. Write four different statements in C++ to do the task. (March – 2015)
Answer:
1) p=p+1;
2) p++;(post increment)
3) ++p; (pre increment)
4) p+=1; (short hand in C++)
Question 3.
Read the following code: (March – 2015)
char str [30];.
cin>>str;
cout<<str;
If we give the input “Green Computing, we get the output “Green. Why is it so? How can you correct that?
Answer:
The input statement cin> cannot read the space. It reads the text up to the space. i.e. the delimiter is space. To read the text up to the enter key getsO or getlineO is used.
Question 4.
Match the following : (March – 2015)
a) Name – Symbol
b) Modulus operator – i) + +
c) Logical operator – ii) ==
d) Relational operator – iii) =
e) Assignment operator – iv) ?:
f) Increment operator – v) &&
g) Conditional operator – vi) %
Answer:
Name | Symbol |
a) Modulus operator | vi) % |
b) Logical Operator | v) && |
c) Relational Operator | ii) == |
d) Assignment operator | iii) = |
e) Increment operator | i) ++ |
f) Conditional Operator | iv) ?: |
Question 5.
Write a C++ expression to calculate the value of the following equation. (Say – 2015)
Answer:
x = (-b + sqrt (b*b – 4*a*c) / (2*a)
Question 6.
A student wants to insert his name and school address in the C++ program that he has written. But this should not affect the compilation or execution of the program. How is it possible? Give an example. (March – 2016)
Answer:
He can use comments to write this information. In C++ comments are used to write information such as the programmer’s name, address, the objective of the codes etc. in between the actual codeš. This is not the part of the program. There are two types of comments
i) Single line (II) and ii) Multi line (/* and */)
i) Single line (a) : Comment is used to make a single line as a comment. It starts with II.
Eg : I/programme starts here.
ii) Multi line (/* and */) : To make multiple lines as a comment. It starts with /* and ends with */
Eg: /* this programme is used to find sum of two numbers */
Question 7.
Consider the following C++ statements : (March – 2016)
char word [20]
cin>>word;
cout<<word;
gets(word);
puts(word);
If the string entered is “HAPPY NEW YEAR”, predict the output and jsutify your answer.
Answer
cin>>word;
cout<<word;
It displays “HAPPY” because cin takes characters upto the space. That is space is the delimiter for cin. The string after space is truncated. To resolve this use gets () function. Because gets () function reads character upto the enter key.
Hence gets (word);
puts (word);
Displays “HAPPY NEW YEAR”
Question 8.
What is a preprocessor directive statement? Explain with an example. (Say – 2016)
Answer:
A C++ program starts with the pre processor directive i.e., # include, #define, #undef, etc, are such a preprocessor directives. By using #include we can link the header files that are needed to use the functions. By using #define we can define some constants.
Eg. #define x 100. Here the value of x becomes 100 and cannot be changed in the program.
No semicolon is needed.
Question 9.
The following C++ code segment is a part of;a program written by Smitha to find the average of 3 numbers. (March – 2017)
int a, b, c;
float avg;
cin>>a>>b>>c;
avg = (a+b+c)/3;
cout<<avg; .
What will be the output if she inputs 1, 4 and 5? How can you correct it?
Answer:
= (1 + 4 + 5)/3
= 10/3
= 3.3333
Instead of this 3.3333 the output will be 3. This is because if both operands are integers an integer division will be occurred, that is the fractional part will be truncated.
To get the correct out put do as follows case 1: mt a,b,c; is replaced by float a,b,c;
OR
case 2:
Replace (a+b+c)/3 by (a+b+c)/3.O;’
OR
case 3:Type casting.
Replace avg=(a+b+c)/3; by avg=(fioat)(a+b+c)/3;
Question 10.
b) Explain the data types’ in C++ (Say-2015)
Answer:
Fundamental data types: It is also called built in data type. They are int, char, float, double and void i) int data type-: It is used to store whole numbers without fractional (decimal point) part. It can be either negative or positive. It consumes 4 bytes (32 bits) of memory.i.e. 232 numbers. That is 231 negative numbers and 231 positive numbers (0 is considered as +ve) So a total of 232 numbers. We can store a number in between -231 to + 2311.
ii) char data type Any symbol from the key board, eg. ‘A’,‘9’,…. It consumes one byte( 8 bits) of memory. It is internally treated as integers, i.e. 28 = 256 characters. Each character is having a ASCII code, ‘a’ is having ASCII code 97 and zero is having ASCII code 48.
iii) float data type It is used to store real numbers i.e. the numbers with decimal point. It uses 4 bytes(32 bits) of memory. Eg. 67.89, 89.9 E-15.
iv) double data type:- It is used to store very large real numbers. It uses 8 bytes(64 bits) of memory.
v) void data type void means nothing. It is used to represent a function returns nothing.
- User defined Data types : C++ allows programmers to define their own data type. They are Structure(struct), enumeration (enum), union, class, etc.
- Derived data types : The data types derived from fundamental data types are called Derived data types. They are Arrays, pointers, functions, etc
Question 11.
Predict the output of the following C++ statements: int a = -5, b = 3, c = 4; (March – 2016)
C+ = a+++ – b;
cout<<a<<b<<c;
Answer:
a = -4,
b = 2 and
c = 1
Question 12.
Write C++ examples for the following: (Say – 2016)
a) Declaration statement
b) Assignment statement
c) Type casting
Answer:
a) int age;
b) age= 16;
c) avg=(float)a+b+c/3;