C/C ++ special exercises-(1) and c/c special exercises

Source: Internet
Author: User

C/C ++ special exercises-(1) and c/c special exercises
Preface

Every Monday of every week is always in poor spirits. write a blog to boost your mind ~
After completing the "integrated exercise for C/C ++ engineers" last time, I wanted to write a few questions if I had nothing to do. I had a good grasp of my skills and had consolidated a lot of basic knowledge. I had to stick to it ~

C/C ++ special exercises (1)

Completion Time:
Total questions: 10
Question preparation time: 20 m
Accuracy: 8
C/C ++ has hundreds of questions, which is a good place to consolidate the foundation of programming languages.
Special exercises focus on the detailed issues in the program design, including many questions about C logic operations, questions about the output results of code segments, and C ++ object-oriented questions.

Incorrect question analysis summary

1. Which of the following code will produce errors during compilation ()

# Include <iostream> using namespace std; struct Foo {Foo () {} Foo (int) {} void fun () {}}; int main (void) {Foo a (10); // Statement 1. fun (); // Statement 2 Foo B (); // Statement 3 B. fun (); // Statement 4 return 0; 16 .}

A. Statement 1
B. Statement 2
C. Statement 3
D. Statement 4

Analysis: this topic describes the struct. The code segment defines the struct type Foo, which contains two constructors of Foo and a common function of foo.
In main, Statement 1 defines a Foo object named a. It will call the Foo (int) {} constructor, correct;
In statement 2, object a is used to call the fun () function of the struct;
In statement 3, this is the key to this question. At first glance, it seems that a struct object named B has been defined. But with careful consideration, how can this form define an object? This seems to be a declaration form of a function. The returned value is the Foo object and the function name is B. But can the main function declare the function internally? Check the information and find that it is really OK. This involves the scope issue. The scope will be different if it is declared in different places, the declaration in the main function can only work in the main function (the declaration in the function is called a local declaration, and the external declaration in the function is called a global declaration ). So there is no problem with this statement.
In statement 4, since Statement 3 is correct, it is a declaration of a local function, so there will be no Foo object named B, so this statement will definitely cause compilation errors!

Answer: D

Conclusion: Incorrect C, insufficient knowledge storage

3. The output result of the following program is ()

char *p1= “123”, *p2 = “ABC”, str[50]= "xyz";strcpy(str+2,strcat(p1,p2));cout << str;

A. xyz123ABC
B. z123ABC
C. xy123ABC
D. Error

Analysis: This question is not about the processing capability of string connection and replication, but about whether char * defines string constants. The p1 and p2 defined in the question are both string constants and read-only, so the answer is clear.
The following describes the problems of string constants and variables:
From the general implementation perspective:
Char s [] = "abcd"; -- when s is a local variable (automatic variable or register variable), it has an automatic storage period and is placed in the stack of runtime memory; when s is a global or local static variable, it has a static storage period and is placed in the static memory area. The string literal "abcd" has a static storage period and is placed in the text constant area. Here, the object "abcd" is used as a right value to initialize object s. The two are not the same.
Char * s = "abcd"; (this usage is deprecated in C ++ and should be replaced by const char * s = "abcd) -- When referencing s, "abcd", and "abcd" by pointer, it is regarded as referencing the same object (which is different in the context of the left-value semantics such as the operands of one dollar and sizeof ), it is generally placed in the text constant area.
The literal constant area is read-only and its semantics is unclear. Therefore, C ++ regards the string literal as a String constant and prohibits the use of pointers to change the literal itself. However, for reasons such as compatibility with old code, in iso c, the act of changing the string literal volume is undefined, although the specific compiler may treat the string literal volume as a constant.

Answer: D

Conclusion: Wrong C is selected, and the question is displayed without thinking about it. It is not only a waste of time, but also a wrong question!

Excellent question favorites

5. The output of the following code is:

#include<iostream>#include<vector>using namespace std;int main(void){    vector<int>array;    array.push_back(100);    array.push_back(300);    array.push_back(300);    array.push_back(500);    vector<int>::iterator itor;    for (itor = array.begin(); itor != array.end(); itor++)    {        if (*itor == 300)        {            itor = array.erase(itor);        }    }    for (itor = array.begin(); itor != array.end(); itor++)    {        cout << *itor << " ";    }    return 0;}

A. 100 300 300 500
B. 100 300 500
C. 100 500
D. program error

Analysis: When the vector and erase functions of vector in the C ++ standard library Delete elements at the specified position, the returned value is an iterator pointing to the next element of the deletion element. After deleting the first 300, the itor points to the second 300, and then executes the itor ++ command to point to 500.

9. In the 80X86 architecture, what is the output value?

union Test {    char a[4];    short b; }; Test test; test.a[0]=256; test.a[1]=255; test.a[2]=254; test.a[3]=253; printf("%d\n",test.b);

A.-128.
B.-256.
C. 128
D. 256.

Analysis: First of all, we need to know the size-end mode. In 80X86, it is the small-end mode. Of course we can write and test it. short occupies 2 bytes, with the Left high address and the right low address;
A [1] a [0]
1111 1111 0000 0000
Short occupies two bytes: a [1] and a [0]. The highest bit is 1, which is a negative number. In the computer, the short uses the complement code. The binary value is: 1000 0001 0000 0000, converted to decimal format-256.

10. What is the output of the following code on a 32-bit machine?

void example(char acWelcome[]){    printf("%d",sizeof(acWelcome));    return;}void main(){    char acWelcome[]="Welcome to Huawei Test";    example(acWelcome);    return;}

A. 0
B. 4
C. 23
D. 24

Analysis: This question examines the use of sizeof (), pay attention to the difference with strlen. Refer to the Encyclopedia of sizeof

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.