C++ Quiz

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What value is printed out for the variable x? #include <iostream> int x; int main() { int y; std::cout << x << std::endl; std::cout << y << std::endl; return 0; }

0 variables with static storage duration are zero initialized. Note that x has static storage duration even though the static keyword is not used. According to the standard: "All objects which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration" 全局变量存在static storage duration,初始化为0

What gets printed? 01 #include <iostream> 02 03 int main() 04 { 05 for (int i = 0; i < 4; ++i) 06 { 07 switch (i) 08 { 09 case 0 : std::cout << "0"; 10 case 1 : std::cout << "1"; continue; 11 case 2 : std::cout << "2"; break; 12 default : std::cout << "D"; break; 13 } 14 std::cout << "."; 15 } 16 return 0; 17 }

0112.D. Based on sections 6.4.2 p6 and 6.6.1 p1 of the standard, The first case label to match the given switch condition is executed,furthermore all subsequent cases (including the default case) will be executed until a break, continue, return or end of switch is encountered. In this case the continue not only exits from the switch but goes directly to the next iteration of the loop. switch中的continue直接跳出了外面的for的一次循环 switch中的break只跳出了switch 因为continue不和switch一起使用的.

What value gets printed by the program? #include <iostream> int main(int argc, char** argv) { int x = 0; int y = 0; if (x++ && y++) { y += 2; } std::cout << x + y << std::endl; return 0; }

1 If the first operand to the logical AND operator evaluates to false the second operand is guaranteed not to evaluate. Note: the logical OR operator is guaranteed not to evaluate the second operand if the first operand is true. Note: for the logical AND and OR operators, all side effects of the first expression, except for destruction of temporaries, happen before the second expression is evaluated.

What value gets printed by the program? #include <iostream> int main(int argc, char** argv) { int x; x = 1, 2, 3; std::cout << x << std::endl; return 0; }

1 The operands to the comma operator are evaluated from left to right. The value of the left hand expression is discarded. The type and value of the result are the type type and value of the right hand operand. Note: assignment takes precedence over the comma operator, so in this case x=1 is evaluated first; than x, 2, 3 赋值优先级高于逗号

Which of the following statements are true? 1) Conversion functions of class A convert from class A into another type 2) Conversion functions of class A are used to convert from another given type into class A 3) Converting constructors must be callable with a single argument

1,3 类型转换函数(type conversion function) 类型转换函数的作用是将一个类的对象转换成另一类型的数据. operator 类型名() {实现转换的语句} 在函数名前面不能指定函数类型,函数没有参数.

What value for y gets printed in the program below? #include <iostream> const int x = 12; int main(int argc, char** argv) { enum dog { x = x, y }; std::cout << y << std::endl; return 0; }

13 An Enumeration's point of declaration is after its definition (which includes the initialization). Therefore in this case the value of the enum 'x' is initialized with the global 'x' which equals 12.

What is the output of the program? #include <iostream> struct A { virtual int foo(int x = 5) { return x * 2; } }; struct B : public A { int foo(int x = 10) { return x * 3; } }; int main(int argc, char** argv) { A* a = new B; std::cout << a->foo() << std::endl; return 0; }

15 The method B::foo is called but with the default argument of 5 from A::foo. "A virtual function call uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides." 虚函数动态绑定,但是默认参数任然用原来函数的. 所以是5*3

Which lines below are ill-formed? 1 #include <iostream> 2 3 struct A 4 { 5 A(int& var) : r(var) {} 6 7 int &r; 8 }; 9 10 int main(int argc, char** argv) 11 { 12 int x = 23; 13 14 A a1(x); 15 16 A a2 = a1; 17 18 a2 = a1; 19 20 return 0; 21 }

18 It is ill-formed to use an implicitly defined assignment operator when one of the members that will need to be copied is a reference. (Line 18)

What gets printed? #include <iostream> struct Car { Car() : price(20000) {} Car(double b) : price(b*1.1) {} double price; }; struct Toyota : public virtual Car { Toyota(double b) : Car(b) {} }; struct Prius : public Toyota { Prius(double b) : Toyota(b) {} }; int main(int argc, char** argv) { Prius p(30000); std::cout << p.price << std::endl; return 0; }

20000 is output. All sub-objects representing virtual base classes are initialized by the constructor of the most derived class. If the constructor of the most derived class does not specify a mem-initializer for a virtual base class V, then V's default construtor is called to initialize the virtual base class subobject. 注意,主要是因为有了virtual,所以默认用base的参数20000, 如果没有virtual,则为33000

What value gets printed by the program? #include <iostream> struct Foo { int x; operator int() { return 21; } }; int main(int argc, char** argv) { Foo f; f.x = 11; std::cout << (0?3:f) << std::endl; return 0; }

21 The 2nd and 3rd operands to the conditional operator must be of the same type. If one of the two can be converted to the other, the conversion will occur. Note: if a conversion exists for each of the operands into the other's type, the program is ill-formed. 这题太缺德,非正常写法.

How many times is Hello World printed by this program? #include <iostream> struct BS { BS() { std::cout << "Hello World" << std::endl; } unsigned int color; }; struct mid1 : virtual public BS { }; struct mid2 : virtual public BS { }; struct mid3 : public BS { }; struct mid4 : public BS { }; struct DR : public mid1, public mid2, public mid3, public mid4 { }; int main(int argc, char** argv) { DR d; return 0; }

3 3 times. One time for the first virtual occurences of BS in the heirarchy and once for each non-virtual occurence of BS. mid1 and mid2 together have one. mid3 and mid4 each have one.

What value does foo print out? #include <iostream> const int SIZE = 5; struct tester { void foo() { std::cout << SIZE << std::endl; } enum { SIZE = 3 }; }; int main(int argc, char** argv) { tester t; t.foo(); return 0; }

3 Names defined at any point in a class are in scope in all member functions of the class. Thus the enum SIZE is in scope in the function foo and hides the global variable SIZE.

What gets printed? #include <iostream> int foo(int i) { return 2; } double foo(double d) { return 4.0; } struct Computer { int foo(int i) { return 8; } }; struct Gateway : public Computer { double foo(double d) { return 16.0; } }; int main(int argc, char** argv) { Gateway g; std::cout << foo(1) + foo(1.0) + g.foo(1) + g.foo(1.0) << std::endl; return 0; }

38 When calling global foo, the function is overloaded and each foo is called once depending on the type of the argument. When calling the member foo, Gateway::foo hides Computer::foo so Gateway::foo is called twice.

What value gets printed by the program? #include <iostream> int foo(int y); int foo(int x) { return x+1; } int main(int argc, char** argv) { int x = 3; int y = 6; std::cout << foo(x) << std::endl; return 0; }

4 The trick here is, we change the parameter names from the function declaration to function definition. This is legal, and only the parameter names in the function definition are used.

What should get printed in the program below? #include <iostream> using namespace std; class foo{ public: foo() : z(x+1), y(2), x(3) { cout << "z: " << z << endl; } private: int x; int y; int z; }; int main(int argc, char** argv){ foo f; return 0; }

4 the value of z is initialized to 4. According the C++ standard: non-static data members shall be initialized in the order they were declared in the class definition, regardless of the order of the mem-initializers. Therefore x is initialized to 3 before z is initialized to X + 1. 按照定义的顺序初始化

What is the value of y at the end of main? const int x = 5; int main(int argc, char** argv) { int x[x]; int y = sizeof(x) / sizeof(int); return 0; }

5 The local x does not hide the global x until the end of the declaration. A local name will hide a global after the end of the declaration but before the beginning of initialization.

What is the output of the program? #include <iostream> class Foo { public: char c; static double sd; double d; int i; }; int main(int argc, char** argv) { Foo f = { 72, 3.14 }; std::cout << f.c + f.d + f.i << std::endl; return 0; }

75.14 "An aggregate is an array or a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions." Aggregates can be initialized by "brace-enclosed, comma separated list of initializer-clauses for the members of the aggregate, written in increasing subscript or member order." Static data members are skipped during this type of initialization so 3.14 initializes d and not sd in this example "If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be value-initialized". So in this example i is value-initialized to 0. 静态变量sd跳过,不予赋值. 没有赋值的默认为0

Which lines below should not compile? 1 struct A 2 { 3 A(int x) : n(x) {} 4 int n; 5 }; 6 7 int main(int argc, char** argv) 8 { 9 A a1; 10 A a2(2); 11 A a3(a2); 12 return 0; 13 }

9 If any user-declared constructor is present in the class, then no default constructor will be created implicitly. Additionally if no user declared copy constructor is declared, then an implicit copy constructor will be created by the compiler. In this example there is a user-declared constructor which prevents the default constructor from existing, but a copy constructor is still created implicitly.

Which of the following operators does NOT indicate a sequence point in the code? && || ? = - correct ,

= All of them except the assignment operator indicate a sequence point.

Which of the following implementations of the reset function is best for initializing the array to all zero. class foo{ public: foo(){ reset(); } private: void reset(){ // A // memset(x, 0, 50); // B // memset(x, 0, sizeof(x)); // C // memset(x, 0, 50 * 4); // D // memset(x, 0, 50 * sizeof(x)); } long x[50]; };

B is the only answer that is portable. C will work on platforms where long is 4 bytes, but many platforms have 8 byte long. sizeof(x)计算整个数组长度.

What gets printed by the code below? 01 #include <iostream> 02 03 class A 04 { 05 public: 06 void foo() const { std::cout << "A"; } 07 }; 08 09 class B 11 { 12 public: 13 void foo() const { std::cout << "B"; } 14 }; 15 16 class C : public A, public B 17 { 18 using A::foo; 19 }; 20 21 int main() 22 { 23 C c; 24 c.foo(); 25 return 0; 26 }

Compiler error on line 24 The accessibility property of the introduced foo method from the A type is private in the C type, hence it will not be publicly accessible. This is the case regardless of the fact that the A type is publicly derived and the foo method has public accessibility in A type. This is further explained in the C++ standard, section 7.3.3 pg 15 by the following: "The alias created by the using- declaration has the usual accessibility for a member-declaration." 使用using,不看继承关系,不看类的内部声明是,就是不许别的类访问.

True or False, the program below will print NOT EQUAL #include <iostream> struct Foo { }; struct Bar { }; int main(int argc, char** argv) { Foo* f = new Foo; Bar* b = new Bar; if ( f == b ) std::cout << "EQUAL" << std::endl; else std::cout << "NOT EQUAL" << std::endl; return 0; }

False Distinct pointer types can not be compared with equality operators

A function call is always an rvalue. True or false?

False A function call can be an lvalue if and only if the return value is a reference

The code below is legal? int x = 5; template <typename T> class x { T member; }; int main(int argc, char** argv) { class x<int> y; return 0; }

False A template class name can not be the same as any other name in the same declarative region. This is also the case for typedef names.

The code below declares and defines variable x extern int x;

False An extern declaration does not define the variable unless it is also initialized in the same statement

In namespace foo, the function bar can access the variable x also declared in namespace foo? #include <iostream> namespace foo { void bar() { x++; } int x; } int main(int argc, char** argv) { return 0; }

False Names used in a namespace must be declared before before their use

Static member functions of classes can be virtual. True or false?

False Static member functions can not be virtual

The code below would declare an array of references, if we had properly initalized the references. True or false? int main() { int& x[50]; return 0; }

False There is no such thing as an array of references. Also there are no references to references or pointers to references.

The variable x is accessible in the else clause of this program? int main(int argc, char** argv) { if ( argc > 2 ) { int x = 5; } else { } return 0; }

False Variables declared in blocks of if,else-if,else are local to the block they were declared in.

What gets printed? #include <iostream> struct A { A() : val() {} A(int v) : val(v) {} A(A a) : val(a.val) {} int val; }; int main(int argc, char** argv) { A a1(5); A a2(a1); std::cout << a1.val + a2.val << std::endl; return 0; }

It is illegal to have a constructor whose first and only non-default argument is a value parameter for the class type. copy构造函数要用A(A &a),第一个参数必须是reference.

True or false? Every expression is an lvalue or an rvalue?

Its true, see Standard 3.10/1

Which lines below should not compile? 1 #include <iostream> 2 3 class Bar 4 { 5 protected: 6 static int x; 7 int y; 8 }; 9 10 int Bar::x = 33; 11 12 class Barrel : public Bar 13 { 14 public: 15 void foo(Bar* b, Barrel* d) 16 { 17 b->y = 0; 18 d->y = 0; 19 Bar::x = 0; 20 Barrel::x = 0; 21 } 22 }; 23 24 int main(int argc, char** argv) 25 { 26 Barrel b; 27 b.foo(&b, &b); 28 return 0; 29 }

Line 17 should not compile. Access to non-static protected members of a base class can not be via a pointer or reference to the base class.

In addition to const_cast, which cast can be used to cast away constness?

None of the above(static_cast, dynamic_cast,reinterpret_cat) Only const_cast (or c-style casts) can be used to cast away constness

What value gets printed by the program? #include <iostream> int main() { int x = 3; switch(x) { case 0: int x = 1; std::cout << x << std::endl; break; case 3: std::cout << x << std::endl; break; default: x = 2; std::cout << x << std::endl; } return 0; }

Nothing, it is ill-formed Jumping into a block and bypassing a declaration with an initialization is ill-formed. In this example there are cases which would bypass the declaration and initialization of block scope x declaration. 实际xcode会得出3

In addition to c-style, which casts can be used to cast an int to a pointer or a pointer to an int?

Only reinterpret_cast (or c-style casts) can be used to cast an int to a pointer or a pointer to an int

In addition to c-style, which casts can be used to cast an int into an enum?

Only static_cast (or c-style casts) can be used to cast an int to an enum

What gets printed by this program? #include <iostream> struct Shape { virtual void print() { std::cout << "SHAPE" << std::endl; } virtual ~Shape() {} }; struct Box : public Shape { virtual void print(int i) { std::cout << "BOX" << std::endl; } }; int main(int argc, char** argv) { Shape* s = new Box; s->print(); delete s; return 0; }

SHAPE SHAPE is printed. A function with a different signature does not override a function in a base class even if the function name is the same.

What gets printed by this program? #include <iostream> struct Shape { virtual Shape* duplicate() { return new Shape; } virtual void print() { std::cout << "SHAPE" << std::endl; } virtual ~Shape() {} }; struct Box : public Shape { virtual Box* duplicate() { return new Box; } virtual void print() { std::cout << "BOX" << std::endl; } }; int main(int argc, char** argv) { Shape* s1 = new Box; Box* b1 = s1->duplicate(); b1->print(); delete s1; delete b1; return 0; }

The program is ill-formed. The return type of the virtual function that is called dynamically is converted to the return type of the overridden function. In this case the result of the duplicate() function is a pointer to a Box object but converted to a Shape pointer. Therefore the assignment from the return value to b1 is ill-formed.

he code below is legal. True or false? #include <iostream> const char* Foo() { return "Hello World"; } int main() { const char* str = Foo(); std::cout << str << std::endl; return 0; }

True String literals have static storage duration, therefore they can be referenced anywhere in the translation unit, even though it is defined in a function

non-const static member variables must be defined outside of the class for them to be used struct test { static int x; }; int test::x;

True The declaration of a static data member in a class does not define it.

The below is legal int x = 5; class x { }; int main(int argc, char** argv) { class x y; return 0; }

True You can have a class and a regular variable with the same name. However the class name will been hidden and the elaborated-type-specfier must be used to access the class name.

Given the code below what will be the output to stdout? #include <cstdio> 00 void foo(int v1, int v2, int v3, int v4) 01 { 02 printf("%d %d %d %d\n",v1,v2,v3,v4); 03 } 04 05 int main() 06 { 07 int lut[] = { 1, 2, 3, 4 }; 08 int idx = 0; 09 foo(lut[idx++],lut[idx++],lut[idx++],lut[idx++]); 10 return 0; 11 }

Undefined As per the standard section 5.4: "Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified58) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined."

Will "Hello World" be printed by the program below? int main(int argc, char** argv) { int array[33]; if ( &array[4] < &array[23] ) { std::cout << "Hello World" << std::endl; } return 0; }

Yes Pointer comparison with greater and less than relational operators is defined within the same array.

Will "Hello World" be printed by the program below? struct Foo { int x; int y; }; int main(int argc, char** argv) { Foo f; if ( &f.x < &f.y ) { std::cout << "Hello World" << std::endl; } return 0; } yes no implementation defined unspecified undefined

Yes Relational operators can be used to do pointer comparison of non-static members in the same object where the definitions of the members is not separated by an access specifier. The member declared first will evaluate to lower. Note: other than this case, the only other time relational operators can be used to compare pointers is when the addresses point to elements in the same array. For all other cases, the result of relational operators on pointers is unspecified.

From line 6 in the code below which labels can you access using a goto statement? 1 2 3 void foo(int x) 4 { 5 pointa: 6 7 pointb: 8 9 pointc: 10 printf("end of function\n"); 11 } 12 13 int main(int argc, char** argv) 14 { 15 foo(5); 16 17 pointd: 18 19 return 0; 20 }

a,b,c Labels have function scope. This means they can be accessed from anywhere within the function where they are defined but not in any other function of the program. Labels are the only construct in C++ with function scope.

Which, if any, of the member function definitions below are ill-formed? #include <iostream> int g_x = 44; struct Foo { int m_x; static int s_x; Foo(int x) : m_x(x) {} int a(int x = g_x) { return x + 1; } int b(int x = m_x) { return x + 1; } int c(int x = s_x) { return x + 1; } }; int Foo::s_x = 22; int main(int argc, char** argv) { Foo f(6); std::cout << f.a() << std::endl; std::cout << f.b() << std::endl; std::cout << f.c() << std::endl; return 0; }

b b is ill-formed. Non-static members can not be used as default arguments.

Which of the following functions are found when called in main during name lookup? #include <iostream> namespace standards { struct datastructure { }; void foo(const datastructure& ds) { } void bar() { } } int main(int argc, char** argv) { standards::datastructure ds; foo(ds); bar(); return 0; }

foo This is called koenig lookup or argument dependent name lookup. In this case, the namespace 'standards' is searched for a function 'foo' because its argument 'ds' is defined in that namespace. For function 'bar', no additional namespaces are searched and the name is not found. More details are in 3.4.2.

What gets printed? #include <iostream> struct mybase { int x; template <int RANGE> virtual void print() { std::cout << RANGE + x + 1 << std::endl; } }; struct myderived : public mybase { template <int RANGE> void print() { std::cout << RANGE + x + 2 << std::endl; } }; int main(int argc, char** argv) { mybase* b = new myderived; b->x = 1; b->print<5>(); return 0; }

ill-formed A member function template can not be virtual.

What value gets printed by the program? #include <iostream> int main() { int sum = 0; int array[3][] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; for (int i = 0; i < 3 ; ++i) { for (int j = 2; j < 3 ; j++) { sum += array[i][j]; } } std::cout << sum << std::endl; return 0; }

ill-formed Only the first constant expression in a multiple dimension array can be ommitted. Therefore the declaration of the array in this example is ill-formed. 只有第一个中括号可以省略数字,第二个不可以省略.

What is the output of the program? #include <iostream> struct Foo { Foo() {} void go() { std::cout << "Foo" << std::endl; } }; struct Bar : public Foo { Bar() {} void go() { std::cout << "Bar" << std::endl; } }; int main(int argc, char** argv) { Bar b; const Foo f = b; f.go(); return 0; }

ill-formed The code is ill-formed. Non-const member functions can not be called on const objects. 非const函数不能被const object 调用.

What value gets printed by the program? #include <iostream> int foo(int x, int y) { return x+y; } int foo(const int x, const int y) { return x+y+1; } int main(int argc, char** argv) { const int x = 3; const int y = 2; std::cout << foo(x,y) << std::endl; return 0; }

ill-formed There can only be one function with the same signature. Altering the cv qualification of parameters does not change the function signature. Therefore the two foo functions have the same signature and the program is ill-formed.

Question #61: What gets printed by this program? #include <iostream> struct Shape { virtual void print() { std::cout << "SHAPE" << std::endl; } virtual ~Shape() {} }; struct Box : private Shape { virtual void print() { std::cout << "BOX" << std::endl; } }; int main(int argc, char** argv) { Shape* s = new Box; s->print(); delete s; return 0; }

ill-formed An implicit conversion from a pointer to a derived class, to a pointer to an inaccessible (private inheritance) base class is ill-formed. Thus the assignment of the new Box to Shape* is ill-formed.

What is the output of the program? #include <iostream> struct BS { unsigned int color; }; struct car : public BS { }; struct truck : public BS { }; struct city : public car, public truck { }; int main(int argc, char** argv) { city c; c.color = 3; std::cout << c.color << std::endl; return 0; }

ill-formed The code is ill-formed. The non-static members of a base class, that is an indirect or direct base class more than once, can not be referred to without qualification.

What value gets printed by the program? #include <iostream> int foo(int x, int y) { return x+y; } double foo(double x, double y) { return x+y; } int main(int argc, char** argv) { double (*ptr)(int, int); ptr = foo; std::cout << ptr(3,8) << std::endl; return 0; }

ill-formed The return type of a function does not alter the signature (for function overloading). However, the return type of a function DOES change the "type" of the function and thus the assignment of foo to ptr in not valid.

What is the value of x at the end of main? int main(int argc, char** argv) { int x = 50 % -7; return 0; }

implementation defined if either operand to a modulus operator is negative the result is implementation defined

Which lines of code below should not compile? 1 struct Foo 2 { 3 }; 4 5 struct Bar 6 { 7 }; 8 9 10 int main() 11 { 12 Foo* f = new Foo; 13 14 Bar* b1 = f; 15 Bar* b2 = static_cast<Bar*>(f); 16 Bar* b3 = dynamic_cast<Bar*>(f); 17 Bar* b4 = reinterpret_cast<Bar*>(f); 18 Bar* b5 = const_cast<Bar*>(f); 19 20 return 0; 21 }

line 14,15,16,18 Only reinterpret_cast can be used to convert a pointer to an object to a pointer to an unrelated object type. The dynamic cast would fail at run-time, however on most compilers it will also fail to compile because there are no virtual functions in the class of the pointer being casted.

Which of the following lines should NOT compile? 1 int main() 2 { 3 int a = 2; 4 5 int* b = &a; 6 7 int const* c = b; 8 9 b = c; 10 11 return 0; 12 }

line 9 int* can be implicity converted to int const* -- 4.4. There is no implicit conversion from int const* to int*. int *=int const * Wrong int const *=int * OK

Which of the following lines should NOT compile? 1 2 int main() 3 { 4 int a[54] = {}; 5 6 int b[54] = {}; 7 8 int* x = a; 9 10 int* const y = a; 11 12 b = x; 13 14 b = y; 15 16 return 0; 17 }

line: 12, 14 Arrays can be implicity converted to pointers without casting -- 4.2. There is no implicit conversion from pointers to arrays. pointer=array OK array=pointer Wrong

Which lines of code below should cause the program to be undefined? 1 struct Foo 2 { 3 virtual ~Foo() {} 4 }; 5 6 struct Bar : public Foo 7 { 8 }; 9 10 int main(int argc, char** argv) 11 { 12 Foo* f = new Bar; 13 delete f; 14 f = 0; 15 delete f; 16 17 Foo* fa = new Bar[10]; 18 delete [] fa; 19 fa = 0; 20 delete fa; 21 22 return 0; 23 }

line: 18 deleting NULL pointers have no effect. deleting a pointer to a base class which points to a derived object is legal assuming the base destructor is virtual. deleting an array of objects using a base class pointer is undefined. 实际上xcode可以通过.

Given the code below, the variable y can be accessed in which blocks of code? 1 int main(int argc, char** argv) 2 { 3 4 if ( argc > 10 ) 5 { 6 7 } 8 else if (int y = argc - 1 ) 9 { 10 11 } 12 else 13 { 14 15 } 16 17 return 0; 18 }

lines 8-15 The variable declared in the else-if did not exist before its declaration and can not be used after the end of the else clause. Variables declared in conditions of if,else-if,else structures can be used in all subsequent conditions and bodies of the if,else-if,else structure.

What value gets printed by the program? #include <iostream> struct Foo { Foo(int d) : x(d) {} int x; }; int main() { double x = 3.14; Foo f( int(x) ); std::cout << f.x << std::endl; return 0; }

nothing, its ill-formed Any construct that could possibly be considered a declaration is a declaration. In this example, the second line of main is interpreted as a function declaration in preference to an object declaration with initialization. 实际xcode得出结果为3

Given the assumptions in the comments, what values is printed for x? int main(int argc, char** argv) { // assume result printed is 4 std::cout << sizeof(int) << std::endl; int x = 0x1000; x = x << 32; std::cout << std::hex << x << std::endl; return 0; } 0x00000000 0xFFFFFFFF implementation defined undefined

undefined The result of a shift operation is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand

What value does size print out? #include <iostream> const int SIZE = 5; struct tester { int array[SIZE]; enum { SIZE = 3 }; void size() { std::cout << sizeof(array) / sizeof(int); } }; int main(int argc, char** argv) { tester t; t.size(); return 0; }

undefined 3.3.6/3 says if reordering member declarations in a class yields an alternate valid program the program is ill-formed. In this case the enum SIZE is not in scope when array is declared making the array size 5. However if the members were reordered to declare array after the definition of the enum the array size would be 3. Thus since reordering member declaration changes the program, it is undefined.

If assumptions in the code below are true what value would be the final value printed by this program? int main(int argc, char** argv) { // assume result printed is 4 std::cout << sizeof(int) << std::endl; int *x = new int; // assume result printed is 0x60000000 std::cout << x << std::endl; std::cout << x + 3 << std::endl; return 0; }

undefined In pointer addition and subtraction if both operands and the result do not point inside the same array (or 1 past the last element of the array) the result is undefined. 实际上再xcode上是ox6000000c

What is the value of the local variable x at the end of main? int x = 5; int main(int argc, char** argv) { int x = x; return 0; }

undefined The local x in main hides the global x before the local x's initializer is considered. Therefore the local x is being initialized with itself (the local uninitialized variable)

Of the variable definitions below, which ones have external linkage and can be accessed from another translation unit? int w = 1; static int x = 2; const int y = 3; extern const int z = 4; int main(int argc, char** argv) { return 0; }

w, z w has external linkage. x has internal linkage. y has internal linkage. z has external linkage 重要! 全局变量前面加了const或者static则会限定在这个文件内. 如果没有则所有源文件都可以见.

What value should be printed for x? #include <iostream> int main() { int x = int() = 3; std::cout << x << std::endl; return 0; }

won't compile int() creates a temporary variable which is an rvalue. The temporary variable that is created can not be assigned to since it is an rvalue. Thus this code should not compile

Which of the following variables can be accessed in foo's function try block handler? void foo(int x) try { int y = 2; throw 1; } catch(int e) { } int main(int argc, char** argv) { foo(3); return 0; }

x and e Function parameters are accessible in the try handler. Function local variables are NOT accessible in the try handler.


संबंधित स्टडी सेट्स

Chapter #10: Recordings - From Bach to Rock and Rap

View Set

Abnormal Psychology Oltmanns Exam 3 - Chapter 9, 10, 11

View Set

Organizational Behavior: Chapter 1-4

View Set

Chapter 13.5 Dynamic Programming

View Set

NCO DLC SET 2 Leadership and Management

View Set

Microsoft Exam 70-480 (HTML5, CSS3, Javascript)

View Set