-
What is the output from this C++ code segment?
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int x = 2, y = 3;
7 cout << x/y << "!";
8
9 double p = 2.0, q = 3.0;
10 cout.setf(ios::fixed);
11 cout.precision(4);
12 cout << p/q << endl;
13
14 return 0;
15 }
- 0!0.6666
- 0!0.6667
- 0.0000!0.6667
- 0.6666!0.6667
next prev start
- Which of the following declares x to be of type double,
results in x having the value 6.5, and has no syntax error?
-
int x = 6.5;
-
int x;
x = 6.5;
-
double x;
x(6.5);
-
double x(6.5);
next prev start
-
What is the type of the constant 0?
- short
- int
- long
- float
- double
next prev start
-
What is the type of the constant 0.0?
- short
- int
- long
- float
- double
next prev start
-
What is the output of the following statement?
cout << "\"Hello\\Goodbye\"";
- Hello\\Goodbye
- "Hello\\Goodbye"
- Hello\Goodbye
- "Hello\Goodbye"
next prev start
-
What is the output of the following statement?
cout << "Hello\nGoodbye";
- Hello\nGoodbye
- HellonGoodbye
- Hello Goodbye
- Hello
Goodbye
next prev start
-
What is the syntax error in the following code?
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 const int x;
7
8 cout << "x = " << x << endl;
9
10 return 0;
11 }
- There is no syntax error.
- A const declaration must include an initialization.
next prev start
-
The following program has no compilation errors. What is
the output?
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int x;
7
8 cout << "x = " << x << endl;
9
10 return 0;
11 }
- x = 0
- x = 4
- x = -858993460
- Not defined, will be: x = some number
next prev start
-
What is the output of the following code?
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int x = 8;
7 double y;
8
9 x++;
10 y = x/8;
11
12 cout.precision(4);
13 cout.setf(ios::showpoint);
14 cout << y << endl;
15
16 return 0;
17 }
- 1.000
- 1.0000
- 1.125
- 1.1250
next prev start
-
If line 13 is removed from the code in the
previous problem, what is the output?
- 1
- 1000
- 1.250
- 1250
next prev start
-
Line 14 has been added and line 10 has been
changed. What is the output?
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int x = 8;
7 double y;
8
9 x++;
10 y = x/8.0;
11
12 cout.precision(4);
13 cout.setf(ios::showpoint);
14 cout.setf(ios::fixed);
15 cout << y << endl;
16
17 return 0;
18 }
- 1.000
- 1.0000
- 1.125
- 1.1250
next prev start
-
If two operands of the division operator / are both of
type int, integer division is used and any fractional part is
discarded even if the result is assigned to a variable of type
double.
A type cast can be inserted by a programmer to force
conversion of one of two int operands of division to be
of type double.
Assume the following
declarations/with initializations of x
and
y
:
int x = 3, y = 2;
double z;
Which of the following will correctly assign the value 1.5 to
z
?
A: z = (double) (x / y);
B:z = (double) x / y;
C: z = ((double) x) / y;
D: z = static_cast<double>(x / y);
E: z = static_cast<double>(x) / y;
- A and D only
- C and E only
- B, C, and E only
- E only
next prev start
-
The user accidentally hits the 'enter' key, then a 5,
'enter' again, then a 10 and 'enter' a third time. What is the
output of the following code?
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int x, y;
7
8 cin >> x;
9 cin >> y;
10
11 cout << "x = " << x << ", y = " << y << endl;
12
13 return 0;
14 }
- x = 0, y = 5
- x = some undefined value, y = 5
- x = some undefined value, y = 10
- x = 5, y = 10
next prev start
-
The variable x
is declared and initialized,
then y
is declared and assigned an expression:
int x = 5;
int y;
y = x++ + 10;
What are the values of x and y after this code executes?
- x = 5, y = 15
- x = 5, y = 16
- x = 6, y = 15
- x = 6, y = 16
next prev start
-
If x = 345
which expression will assign the
value 4 to y
?
y = x / 10;
y = x % 10;
y = (x / 10) % 10;
y = (x % 10) / 10;
next prev start
-
Which of the following expressions will output the
value of an int
variable x
on one line
and an int
variable y
on the next
output line?
A: cout << x;
cout << y;
B: cout << x << endl;
cout << y << endl;
C: cout << x << y;
D: cout << x << endl << y << endl;
- A, B, and D only
- B and D only
- B only
- A, B, C, and D
next prev start
-
The << operator is overloaded - it can
either be the output insertion operator or the left-shift
operator. If both operands are of type int
, it is
the left-shift.
What is the printed output of this code?
#include <iostream>
using namespace std;
int main()
{
int x = 5;
int a,b;
a = x << 1;
b = x << 2;
cout << a << ", " b << endl;
return 0;
}
- 10, 20
- 51, 52
- 6,7
- 5,10
next prev start
-
What is the output of the following C++ program?
#include <iostream>
using namespace std;
int main()
{
int x = 5;
int y = 2;
cout << x << y << " ";
cout << (x << y) << endl;
return 0;
}
- 5 2 5 2
- 52 52
- 5 2 20
- 52 20
prev start