C ++ basic knowledge interview featured 100 question series (1-10 questions) [C ++ basics]

Source: Internet
Author: User

[Original article link]

Http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-1-10.html

Question1]

We can use static to modify the member functions of a class, or use const to modify the member functions of the class. (It is written at the end of the function to indicate that the member variables cannot be modified, it does not mean that the return value is a constant ). Can I modify the member functions of the class with both static and const?

Analysis]

No. When implementing a const member function, the C ++ compiler adds an implicit parameter const this * to ensure that the function cannot modify the instance status of the class *. But when a Member is static, this function does not have the this pointer. We can also understand that the meanings of the two are contradictory. Static is for the type and has no relationship with the instance of the class, while const is for the instance, so that the function cannot modify the instance status. Therefore, they cannot be used at the same time.

Question2]

Run the following code. What is the output?

C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  Class
{
};

Class B
{
Public:
B (){}
~ B (){}
};

Class C
{
Public:
C (){}
Virtual ~ C (){}
};

Int _ tmain (int argc, _ TCHAR * argv [])
{
Printf ("% d, % d, % d \ n", sizeof (A), sizeof (B), sizeof (C ));
Return 0;
}

Analysis]

The answer is 1, 1, and 4.

Class A is an empty type, and its instance does not contain any information. Originally, sizeof should be 0. But when we declare this type of instance, it must occupy a certain amount of space in the memory, otherwise these instances cannot be used. The compiler determines how much memory is used. In Visual Studio 2008, each empty instance occupies one byte space.

Class B adds constructor and destructor Based on class. The call of constructor and destructor is independent of the Instance type (you only need to know the function address to call them), so no information is required in the instance. So sizeof (B) is the same as sizeof (A), and is 1 in Visual Studio 2008.

Class C marks the Destructor as a virtual function based on class B. The C ++ compiler generates a virtual function table for a type that contains virtual functions, and adds a pointer to the virtual function table to each instance of the type. On a 32-bit machine, a pointer occupies 4 bytes of space, so sizeof (C) is 4.

Question3]

Run the following code. What is the result?

C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  # Include "stdafx. h"

Class
{
Private:
Int m_value;

Public:
A (int value)
{
M_value = value;
}
Void Print1 ()
{
Printf ("hello world ");
}
Void Print2 ()
{
Printf ("% d", m_value );
}
};

Int _ tmain (int argc, _ TCHAR * argv [])
{
A * pA = NULL;
PA-> Print1 (); // "hello world"
PA-> Print2 (); // ERROR

Return 0;
}

Analysis]

The answer is that the Print1 call is normal and the hello world is printed, but the program crashes when it runs to print2. When you call Print1, you do not need the pA address because the function address of Print1 is fixed. The compiler will pass in a this pointer to Print1, Which is NULL, but this pointer is not used in print1. As long as the program is running without accessing the memory that shouldn't be accessed, no error occurs, so it runs normally. When running print2, this pointer is required to obtain the m_value value. The program crashes because this pointer is NULL.

Question4]

Run the following code. What is the result?

C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  Class
{
Private:
Int m_value;

Public:
A (int value)
{
M_value = value;
}
Void Print1 ()
{
Printf ("hello world ");
}
Virtual void Print2 ()
{
Printf ("hello world ");
}
};

Int _ tmain (int argc, _ TCHAR * argv [])
{
A * pA = NULL;
PA-> Print1 ();
PA-> Print2 ();

Return 0;
}

[Analysis]

The answer is that the Print1 call is normal and the hello world is printed, but the program crashes when it runs to print2. The call of Print1 is the same as that of the previous question. Because Print2 is a virtual function. When calling a virtual function, C ++ obtains the virtual function table based on the virtual function table pointer in the instance (that is, the instance pointed to by this pointer), and finds the function address from the virtual function table. Because this step needs to access the instance address (that is, the this pointer), and this pointer is a null pointer, this causes memory access errors.

Question5]

Can static member functions be also virtual functions?

[Analysis]

The answer is no. Do not call static member functions for instances. However, to call a virtual function, you must obtain a pointer to the virtual function table from an instance to obtain the function address. Therefore, you need an instance to call a virtual function. The two are in conflict.

 Question6]

Run the following C ++ code. What is output?

C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  # Include "stdafx. h"

Struct Point3D
{
Int x;
Int y;
Int z;
};

Int _ tmain (int argc, _ TCHAR * argv [])
{
Point3D * pPoint = NULL;
Int offset = (int) (& pPoint-> z );

Printf ("% d", offset); // 8
Return 0;
}
/*
+ PPoint 0x00000000 {x = ??? Y = ??? Z = ??? } Point3D *
+ & PPoint-> x 0x00000000 int *
+ & PPoint-> y 0x00000004 int *
+ & PPoint-> z 0x00000008 int *
*/

[Analysis]

Output 8. & (PPoint-> z) indicates the address of the variable z in pPoint (pPoint address 0 and z offset 8), and does not need to access the memory pointed to by pPoint. As long as you do not access the invalid memory, the program will not go wrong.

 Question 7]

Run the following C ++ code. What is output?

C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  # Include "stdafx. h"

Class
{
Public:
A ()
{
Print ();
}
~ A ()
{
Printf ("A is erased. \ n ");
}
Virtual void Print ()
{
Printf ("A is constructed. \ n ");
}
};

Class B: public
{
Public:
B ()
{
Print ();
}
~ B ()
{
Printf ("B is erased. \ n ");
}
Virtual void Print ()
{
Printf ("B is constructed. \ n ");
}
};

Int _ tmain (int argc, _ TCHAR * argv [])
{
A * pA = new B ();
Delete pA;

B * pB = new B ();
Delete pB;

Return 0;
}
/*
A is constructed.
B is constructed.
A is erased.

A is constructed.
B is constructed.
B is erased.
A is erased.
*/

[Analysis]

The output result is shown above.

[Question 8]

Run the following C # code. What is the output?

C # Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  Namespace ChangesOnString
{
Class Program
{
Static void Main (string [] args)
{
String str = "hello ";
Str. ToUpper ();
Str. Insert (0, "WORLD ");

Console. WriteLine (str );
}
}
}

[Analysis]

The output is hello. In. NET, String has a special property: the status of the String instance cannot be changed. If the String member function modifies the instance status, a new String instance is returned. Changes will only appear in the return value, but will not change the original Instance. Therefore, the output in this question is still the original string value hello. If you try to change the String content, you can obtain the changed value through the return value. Using StringBuilder is a better choice, especially when it needs to be modified multiple times in a row. If the String is used for multiple consecutive modifications, each modification generates a temporary object with a high overhead.

[Question 9]

In C ++ and C #, what is the difference between struct and class?

[Analysis]

In C ++, if the access permission level of the function or variable is not specified, it is public in struct, while private in class.

In C #, if the access permission level of the function or variable is not specified, both struct and class are private.

The difference between struct and class is that struct defines the value type, and its instance allocates memory on the stack; class defines the reference type, and its instance allocates memory on the stack.

 [Question 10]

In the running C # code, what is the output?

C # Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  Namespace StaticConstructor
{
Class
{
Public A (string text)
{
Console. WriteLine (text );
}
}

Class B
{
Static A a1 = new A ("a1 ");
A a2 = new A ("a2 ");

Static B ()
{
A1 = new A ("a3 ");
}

Public B ()
{
A2 = new A ("a4 ");
}
}

Class Program
{
Static void Main (string [] args)
{
B B = new B ();
}
}
}

[Analysis]

Print four rows, namely a1, a3, a2, and a4.

Execute the static constructor of B before calling the code of type B. Static functions initialize static variables and then execute statements in static functions. Therefore, print a1 first and then a3. Next, execute B B = new B (), that is, call the normal constructor of B. The constructor first initializes the member variables and executes the statements in the function body. Therefore, a2 and a4 are printed successively.

[Reference]

Http://zhedahht.blog.163.com/blog/static/254111742011012111557832/

[Original article link]

Http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-1-10.html

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.