C + + Special Practice-(2)

Source: Internet
Author: User

Order

C + + Foundation Special exercise Two,,, level still not home!

Analysis and summary of the wrong problem

1. The following template definitions are available:

T>T fun(T x,T y){    return x*x+y*y;}

In the following call to fun, the error is ()
A. Fun (1, 2)
B. Fun (1.0, 2)
C. Fun (2.0, 1.0)
D. Fun (1, 2.0)

Analysis: This is a C + + template of the topic, template function call, here is the main requirement of the function parameter return value of the same type;
For option A, the parameters are int type, conform to the requirements, option B, the parameters are floating point type and integer type, the type is inconsistent, error, option C, the parameters are floating point type, the type is the same; option D, by forcing type conversion, converts the parameters to floating point type, and the types are consistent
Answer: B
Summary: The wrong choice D, the function of the template to understand the call is not thorough;

2. In C + +, declare the const int I, at which stage do I read-only?
A. Compiling
B. Links
C. Running
D. None of the above is correct

Analysis: const is used to indicate that the defined variable is read-only. These are completed during compilation, and the compiler may use constants to directly replace references to this variable.
Answer: A
Summary: Wrong choice C

3. The overload, overwrite, and hide differences for class member functions are described correctly.
A. Overwrite refers to the same name in the same class, with different parameters
B. Overloading refers to a derived class function that overrides a base class function with the same function, with the same parameters, and the base class function must have the virtual keyword
C. A derived class function is the same as a base class function, but a different parameter will "hide" the parent class function
D. Functions with the same name and same parameters as the base class without the virtual keyword "Hide" the parent class function

Analysis: The overload, overwrite, and hide concepts for class member functions are very similar, as summarized below:
A. Features that are overloaded with member functions:
(1) The same range (in the same class);
(2) The function has the same name;
(3) different parameters;
(4) The virtual keyword is optional.
B. Overwrite refers to a derived class function overriding a base class function, characterized by:
(1) different ranges (in the derived and base classes, respectively);
(2) The function has the same name;
(3) the same parameters;
(4) The base class function must have the virtual keyword.
C. " Hide refers to a function of a derived class that masks a base class function with the same name as the following rule:
(1) If the function of the derived class has the same name as the function of the base class, but the parameters are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (Note that it is not confused with overloading).
(2) If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (be careful not to confuse the overlay)
Answer: CD This question is multiple choice

4. The following program will be executed after the error or effect:

#define MAX 255int main(){     char A[MAX], i;     for0; i <= MAX; i++)         A[i] = i;}

A. Array out-of-bounds
B. Cycle of Death
C. Stack Overflow
D. Memory leaks

Analysis: Dead loop addend Group cross-border access (C + + does not do array cross-check max=255;
(1) The subscript range of array A is: 0....max-1;
(2) When I loop to 255, the loop executes: a[255]=255; the sentence itself is fine, but when the for (i=0;i<=max;i++) statement is returned, because unsigned char has a range of values (0..255), i++ i is 0, This will be infinite cycle, resulting in a cycle of death;
Answer: AB

5. What is the right thing to say about static use?
A. Declaring A static external class
B. Declaring a static external global variable
C. Declaring a static external function
D. Declaring static local variables

Analysis: Type declarators have three uses in the C language:
1: Set a static local variable, the variable is defined only once and cannot be used by another function
2: Set static global variables, variables cannot be used by external files
3: Set static function in class, Access static variable only
Answer: BCD

6. Please find out all the errors in the code below. Description: The following code is the reverse of a string, such as "ABCD" after the reverse into "DCBA"?

1 #include "string.h"2 intMain ()3{4    Char*SRC ="Hello,world";5    Char*dest = NULL;6    intLen =strlen(SRC);7Dest = (Char*)malloc(LEN);8    Char*d = dest;9    Char*s = Src[len];Ten    while(len--! =0) Oned++ = s--; A   printf('%s ', dest); -   return 0; -}

A. Line 7th to allocate a space for '% '
B. Line 9th to char * s = &src[len-1]
C. Add *d = 0 before line 12th;
D. Add free (dest) space before line 13th

Analysis:

intMain () {Char*SRC ="Hello,world";intLen =strlen(SRC);Char*dest = (Char*)malloc(Len +1);//To allocate a space for the    Char*d = dest;Char*s = &src[len-1];//point to last character     while(len--! =0) *d++ = *s--; *d =0;//tail to add    printf("%s\n", dest); Free(dest);//After use, you should free up space to avoid leakage of memory sink    return 0; }

Answer: ABCD

Collection of fine topics

1. What is the output of the following section of C + + code?

#include "stdio.h"Class Base { Public:int Bar(Charx) {return(int) (x); }Virtual intBar (intx) {return(2* x); } }; Class Derived: PublicBase { Public:int Bar(Charx) {return(int) (-X); }intBar (intx) {return(X/2); } };intMainvoid) {Derived Obj;     Base *pobj = &Obj; printf"%d,", Pobj->bar (Char)( -))); printf"%d,", Pobj->bar ( -)); }

A. 100,-100
B. 100,50
C. 200,-100
D. 200,50
Analysis:
Derived OBJ;
Base *pobj = &Obj;
printf ("%d,", Pobj->bar ((char) (100)))
printf ("%d,", Pobj->bar (100));
The first bar (char) is a non-virtual function, so it is a static binding, which refers to the object when the pointer points to the declaration, the base class when the pobj is declared, and therefore calls the base class bar (char)
The second bar (char) is a virtual function, so it is dynamic bound, which refers to the pointer to the referenced object, pobj references the derived object, and therefore calls the bar (int) of the derived class.

C + + Special Practice-(2)

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.