A good C ++ exam

Source: Internet
Author: User

1.
What is displayed when F () is called given the Code:

Class number {
Public:
String type;
Number (): type ("Void "){}
Explicit number (short): type ("short") {}// explicitly constructed here to prevent implicit conversion.
Number (INT): type ("int "){}
};

Void show (const number & N) {cout <n. type ;}

Void F ()
{
Short S = 42;
Show (s); // first, the Data Type of S must be automatically converted to the int type. Therefore, the number (INT) constructor is used.
// The result is C.
}

A) void
B) Short
C) int
D) None of the above

2. Which is the correct output for the following code

Double darray [2] = {4, 8}, * P, * q;
P = & darray [0];
Q = p + 1;
Cout <q-P <Endl; // output 1
Cout <(INT) q-(INT) P <Endl; // output 8
// Select a. The reason is very simple.

A) 1 and 8
B) 8 and 4
C) 4 and 8
D) 8 and 1

3. which set of statements is not legal C ++?
A) int x = 4; const Int & Y = X;
B) float F = 2.4; short S = F;
C) Char c = 'a'; const char * const P = & C;
D) struct s {int M, N ;}q = {0 };
E) All are valid.
Resolution:
Obviously, the answer is E.
4. What is the output of the below program?

Int main (void)
{
Int A = 10, B = 20, C;

C = A ++ B; // here the compilation error is generated because the non-lvalue cann't increment.
// C = A ++ (++ B). The output is 31. Therefore, the answer before the correction is B.
Cout <C <Endl;

Return 0;
}

A) 31
B) Compilation Error
C) 30
D) 32

5. What is the output of the following program?

# Define max (A, B) (a)> = (B ))? (A): (B ))

Int main (void)
{
Int A = 10, B = 9;

Cout <max (A --, ++ B) <Endl; // output 9 here, because cout <(A --) <Endl;
Cout <A <Endl; // The output here is 8

Return 0;
}
Resolution:
This question is also relatively simple. Two auto-increment or auto-subtraction operations are performed on macros.
As mentioned above, after the cout occurs, output 9. The answer is B.
A) 8
B) 9
C) 10
D) 11

6. the keyword volatile in the following code...

Volatile unsigned x = getvalue ();

A )... Ensures thread safety.
B )... Is obsolete and irrelevant.
C )... Sets X to the return value of getvalue ().
D )... None of the above.

Resolution:
The volatile keyword is an implementation-dependent modifier, used when declaring variables, which prevents the compiler from optimizing those variables. volatile shocould be used with variables whose value can change in unexpected ways (I. e. through an interrupt), which coshould conflict with optimizations that the compiler might perform.
I have some questions about this question. Even the answer is.

7. What is the output of the following program?

Int main (void)
{
Int * pnumber;
Pnumber = new int;

* Pnumber = 10;

Cout <++ * pnumber ++ <Endl; // output 11
Cout <* pnumber <Endl; // the pointer pointing here is ambiguous and the estimated value is random.

Return 0;
}

A) 10 and 11
B) 11 and 12
C) 10 and 0
D) 11 and 0

Resolution:
I tried it on the compiler. The answer is D.
However, I think that cout <* pnumber <Endl; will output a random value because pnumber ++ is ambiguous after the preceding output statement.

8. Find out the error in the code below, if any

# Define public private // (1)

Class animal {
Public: // (2)
Void makenoise ();
};

Int main (void)
{
Animal animal;
Animal. makenoise (); // (3)
Return 0;
}

A) error at line 1
B) error at Line 2
C) error at line 3
D) error at all locations 1, 2, and 3

Resolution:
This is a simple question. The answer is that the C. Object cannot directly access private members, but must be accessed through an interface, that is, a public member function.

9. template <class T, int size = 10>
Class Array {
...
...
};
 
Void Foo ()
{
Array <int> arr1;
Array <char> arr4, arr5;
Array <int> arr2, arr3;
Array <double> arr6;
... ...
... ...
}

How many instances of the template class array will get instantiated inside the function Foo ()

A) 3
B) 6
C) 4
D) 1

Resolution:
Khan: I don't know much about the template. I guess one.
The answer is a. The idea is array <int> arr2. arr3 will use the instance of array <int> arr1. Therefore, it will not be generated repeatedly, that is, three instances in total.

10.
Class Test
{
Public:
Test (){}
Test (char * Name, int Len = 0) {}// F1
Test (char * Name) {} // F2
};

Int main ()
{
Test OBJ ("hello ");
Return 0;
}

Which of the following statement is correct for the above stated code sample
 
A) will generate run time error
B) will generate compile time error.
C) will execute successfully
D) none of the above.

Resolution:
The answer to this question is B.
Use F1 and F2 to replace the above two functions. Fuzzy semantics is generated during compilation.
C ++ has the idea that the potential ambiguity is not a mistake.
The compiler cannot think of calling that function. Of course, only generate compile time error is allowed.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.