logo

Crowdly

Browser

Add to Chrome

met2502f25-a.sg

Looking for met2502f25-a.sg test answers and solutions? Browse our comprehensive collection of verified answers for met2502f25-a.sg at distance3.sg.digipen.edu.

Get instant access to accurate answers and detailed explanations for your course questions. Our community-driven platform helps students succeed!

Determine the exact text printed to standard output stream by the following code fragment?

namespace A {

char c{'a'};

}

char c{'b'};

int main() {

std::cout << c;

}

0%
0%
0%
View this question
View this question

Does the following code fragment compile?

namespace helpers { int counter{1}; }

namespace helpers { int strength{2}; }

int main() {

std::cout << helpers::counter << "\n";

std::cout << helpers::strength << "\n";

std::cout << helpers::Div2(8) << "\n";

}

namespace helpers {

int Div2(int value) { return value/2; }

}

0%
View this question

Does the following code fragment compile?

namespace helpers {

int counter{1}, strength{2};

}

int Div2(int value) {

return value/2;

}

int main() {

std::cout << helpers::counter << "\n";

using helpers::strength;

std::cout << strength << "\n";

std::cout << Div2(8) << "\n";

}

0%
0%
View this question

Determine the value written to the standard output stream by the following code fragment:

int i = 5, *p1 = &i;

*p1=*p1**p1;

std::cout << *p1;

Write if the code doesn't compile. Write if the code causes undefined behavior at run time. Otherwise, write the exact text written to the standard output stream.

View this question
View this question

Like C, C++ is designed to let programs get close to the hardware when necessary. The arithmetic types are defined to cater to the peculiarities of various kinds of hardware. Accordingly, the number of arithmetic types in C++ can be bewildering. Most programmers can [and should] ignore these complexities by restricting the types they use. A few rules of thumb can be useful in deciding which type to use:

  • Use an unsigned type when you know that the values cannot be negative.
  • Use int for integer arithmetic. short is usually too small and, in practice, long often has the same size as int. If your data values are larger than the minimum guaranteed size of an int, then use long long.
  • Do not use plain char or bool in arithmetic expressions. Use them only to hold characters or truth values. Computations using char are especially problematic because char is signed on some machines and unsigned on others. If you need a tiny integer, explicitly specify either signed char or unsigned char.
  • Use double for floating-point computations. float usually doesn't have enough precision, and the cost of double-precision calculations versus single-precision is negligible. In fact, on some machines, double-precision operations are faster than single. The precision offered by long double usually is unnecessary and often entails considerable run-time cost.

Now, determine the value written to the standard output stream by the following code fragment:

unsigned sum {};

for (unsigned u = 5; u >= 0; --u) {

sum += u;

}

std::cout << sum;

Write if the code doesn't compile. Write if the loop in the code fragment doesn't terminate. Write if the code causes undefined behavior at run time. Otherwise, write the exact text written to the standard output stream.

View this question
View this question

Given the expression: 

n = 10

that has both rvalue and lvalue expressions, a useful heuristic to determine whether an expression is lvalue is to ask if you can take its address. If you can, it typically is a lvalue expression. If you can't, it's usually a rvalue expression. So, in the expression n = 10, we have one lvalue expression n and two rvalue expressions: 10 and n=10.

Just remember that in C, built-in pre-increment/decrement operators, comma operator, ternary or conditional ?: operator evaluate to an rvalue. In C++ these operators evaluate to an lvalue.

When the expression is a function call or a call to an overloaded operator, the answer to the question what kind of value? is not so obvious. If the definition or declaration is available, it's possible to answer the question. For other expressions such as ++i involving the built-in pre-increment operator, you now know that the operator requires an lvalue operand and evaluates to an lvalue expression.

Consider the following C++ code. Write if the code doesn't compile. Write if the code causes undefined behavior at run time. Write if an unspecified value is written to standard output. Otherwise, write the exact text written to the standard output stream.

int main() {

int i{};

++i = 5;

std::cout << i;

}

View this question

What is your opinion of the following code fragment?

int foo(int n) {

const int ci {n};

int array[ci] = { ci, array[0]+1, array[1]+2, array[2]+3};

return array[0]+array[1]+array[2]+array[3];

}

0%
0%
0%
0%
0%
View this question

Want instant access to all verified answers on distance3.sg.digipen.edu?

Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!

Browser

Add to Chrome