1. 1. Consider the following code snippet: cout << \"Please enter a number: \";
ID: 3535929 • Letter: 1
Question
1.
1.
Consider the following code snippet:
cout << "Please enter a number: ";
int x;
cin >> x;
cout << "Please enter another number: ";
int y;
cin >> y;
cout << "The sum is " << x + y << endl;
Suppose the student provides the answer 17 29 [Enter] as input to the first prompt. What will the program do?
Answer
Print "The sum is 17"
Print "The sum is 46"
Produce an error because the input for the first prompt is not a single integer
Produce an error because no value was provided after the second prompt
Question 26
1.
Which of the following statements is correct about constants?
Answer
Constants are written using capital letters because the compiler ignores the constants declared in small letters.
The data stored inside a const variable can be changed using an assignment statement.
You can make a variable constant by using the constant keyword while declaring the variable.
Constant variables make a code snippet more readable and easy to maintain.
Question 27
1.
Which of the following options is valid with reference to the code snippet?
#include <iostream>
using namespace std;
int main()
{
double d = 45.326;
double r = d % 9.0;
cout << r;
return 0;
}
Answer
The value inside the variable r will be 0.326
The value inside the variable r will be 5.036
Variable r has to be defined as an integer as the % operator always returns an integer
The assignment statement for variable r is wrong, as the % operator expects integer values as operands
Question 28
1.
Consider the following C++ variable names:
I. 1st_instance
II. basic_in_$
III. _emp_name_
IV. address_line1
V. DISCOUNT
Which of the following options is correct?
Answer
Only IV is a valid C++ variable name.
Only I and IV are valid C++ variable names.
Only I, IV, and V are valid C++ variable names.
Only III, IV, and V are valid C++ variable names.
Question 29
1.
What is the output of the following code snippet?
int main()
{
double a;
a = sqrt(9.0) + sqrt(16.0);
cout << a << endl;
return 0;
}
Answer
25.0
34
7.0
Compilation error
Question 30
1.
What is the result of the following statement?
string s = "New" + " " + "Jersey";
Answer
The string s has the following value: "New Jersey"
The statement results in an error because the + operator can be used only with numbers.
The statement results in an error because the + operation cannot be performed on string literals.
The string s has the following value: "NewJersey"
1.
Which one of the following statements gives the absolute value of the floating-point number
x = -25.50 in C++?
Answer
abs(x);
fabs(x);
x.abs();
x.fabs();
Question 32
1.
Which one of the following is an assignment statement?
Answer
int a = 20;
a = 20;
assign a = 20;
assign 20 to a;
Question 33
1.
What is the value of pow(2, 3)?
Answer
5
6
8
9
1.
What happens to the fractional part, while performing a division on two integer variables?
Answer
The result is rounded off to the nearest integer value.
The fractional part is discarded.
Two integers cannot be used in division, at least one of the operand should be a floating point number.
Instead of using an integer division, you should use the modulus operator if you want to perform floating point division.
Question 35
1.
Consider the following division statements:
I. 22 / 7
II. 22.0 / 7
III. 22 / 7.0
IV. 22.0 / 7.0
Which of the following options is correct?
Answer
All the four statements will return an integer value.
Only I will return an integer value.
Only I, II, and III will return an integer value.
Only II and III will return an integer value.
Question 36
1.
What is the output of the following code snippet?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int var1 = 10;
int var2 = 2;
int var3 = 20;
var3 = var3 / (var1 % var2);
cout << var3 << endl;
return 0;
}
Answer
0
4
20
There will be no output due to a run-time error.
Question 37
1.
Which of the following statements are valid about strings?
I. An empty string literal is represented as "".
II. A string stored in a string variable can be changed.
III. String variables are initialized automatically.
IV. A string variable can contain numbers and special characters.
Answer
Only II and III are valid.
Only III and IV are valid.
Only I and III are valid.
All four statements are valid.
Question 38
1.
Assuming that a user enters 64 as his marks obtained, what is the output of the following code snippet?
int main()
{
int score = 0;
cout << "Enter your score: ";
cin >> score;
if (score < 40) { cout << "F" << endl; }
else if (score >= 40 || score < 50) { cout << "D" << endl; }
else if (score >= 50 || score < 60) { cout << "C" << endl; }
else if (score >= 60 || score < 70) { cout << "B" << endl; }
else if (score >= 70 || score < 80) { cout << "B+" << endl; }
else { cout << "A" << endl; }
return 0;
}
Answer
D
C
B
A
Question 39
1.
What is the output of the following code snippet, if the input is 50?
#include <iostream>
using namespace std;
int main()
{
int x;
cout << "Please enter a digit: ";
cin >> x;
if (x > 100)
{
x++;
}
else
{
x--;
}
cout << x << endl;
return 0;
}
Answer
49
50
51
52
Question 40
1.
Assuming that a user enters 15 as input, what is the output of the following code snippet?
int main()
{
int number;
cout << "Please enter a number: ";
cin >> number;
if (number > 20)
{
cout << "The number is LARGE!" << endl;
}
else
{
cout << "The number is SMALL!" << endl;
}
return 0;
}
Answer
There is no output due to compilation errors.
The number is LARGE!
The number is SMALL!
The number is LARGE!
The number is SMALL!
Question 41
1.
What is the value of the price variable after the following code snippet is executed?
int price = 42;
if (price < 40) { price = price + 10; }
if (price > 30) { price = price * 2; }
if (price < 100) { price = price - 20; }
Answer
42
52
84
64
1.
Consider the following code snippet:
int score = 0;
double price = 100;
if (score > 0 && price < 200 && price / score > 10)
{
cout << "buy" << endl;
}
Which of the following statements is true on the basis of this code snippet?
Answer
The output is buy.
The code snippet compiles and runs, but there is no output.
The code snippet doesn't compile.
The code snippet causes a divide-by-zero error.
Question 43
1.
Which of the following code snippets would you use to test for equality of variables a and b?
Answer
if (a = b) { ...... }
if (a == b) { ...... }
if (a != b) { ...... }
if (a ! b) { ...... }
Question 44
1.
What is the output of the following code snippet?
#include <iostream>
using namespace std;
int main()
{
int s1 = 20;
if (s1 <= 20)
{
cout << "1";
}
if (s1 <= 40)
{
cout << "2";
}
if (s1 <= 20)
{
cout << "3";
}
return 0;
}
Answer
1
2
3
123
Question 45
1.
Which of the following statements is true about an if statement?
Answer
It guarantees that several statements are always executed in a specified order.
It repeats a set of statements as long as the condition is true.
It allows the program to carry out different actions depending on the value of a condition.
None of the listed options is true.
1.
Which of the following operators are computed lazily?
Answer
++
--
&&
==
Question 47
1.
Which of the following variables is used to store a condition that can be either true or false?
Answer
Algebraic
Logical
Boolean
Conditional
Question 48
1.
What is the output of the following code snippet?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "her";
string str2 = "cart";
if (str1 < str2)
{
cout << str2;
}
else
{
cout << str1;
}
return 0;
}
Answer
her
hercart
cart
There is no output due to compilation errors.
1.
Assuming that the user provides 100 as input, what is the output of the following code snippet?
int a, b;
a = 0;
cout << "Please enter b: ";
cin >> b;
if (b >= 100);
{
a = b;
}
cout << "a: " << a << endl;
Answer
a: 0
a: 99
a: 100
There is no output due to compilation errors.
Question 50
1.
Assuming that the user provides 300 as input, what is the output of the following code snippet?
int x;
int y;
x = 0;
cout << "Please enter y: ";
cin >> y;
if (y > 303) { x = y; }
else { x = 0; }
cout << "x: " << x << endl;
Answer
x: 0
x: 300
x: 303
There is no output due to compilation errors.
Print "The sum is 17"
Print "The sum is 46"
Produce an error because the input for the first prompt is not a single integer
Produce an error because no value was provided after the second prompt
Explanation / Answer
www.indiabix.com/cpp-programming/questions-and-answers/‎
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.