Summary of interview questions (1)

Source: Internet
Author: User

1. Is a parent class having written a virtual function? Can a sub-class overwrite its function without the virtual function realize polymorphism?

Virtual modifiers are implicitly inherited. Private is also integrated, but the derived class has no access permission. Virtual instances can be added or not added. All variables of the parent class (except static) exist in the space of the Child class ). Only one entity exists in a function (except inline ). The sub-class can also implement polymorphism without adding virtual to its function. In the subclass space, there are private variables of the parent class. Private variables cannot be accessed directly.

--------------------------------------------------------------------------
2. Enter a string and output it in reverse order. (C ++ is used, and pseudo code is not recommended)

# Include <iostream>
Using namespace STD;

Void main ()
{
Char A [50]; memset (A, 0, sizeof ());
Int I = 0, J;
Char T;
Cin. Getline (A, 50, '\ n ');
For (I = 0, j = strlen (a)-1; I <strlen (a)/2; I ++, j --)
{
T = A [I];
A [I] = A [J];
A [J] = T;
}
Cout <A <Endl;
}

// Type 2

String STR;
Cin> STR;
Str. Replace;
Cout <STR;

--------------------------------------------------------------------------
3. Briefly describe the Windows Memory Management Method.

Memory Management is an important part of the operating system. I am afraid no one can make it clear in two or three sentences ~~
Let me give you a rough idea. I hope you can give me some advice.

When running the program, you need to read the code of this program from the memory. The location of the Code must be in the physical memory before it can be run. Because there are a lot of programs running in the operating system, the memory cannot be completely put down, so the concept of virtual memory is introduced. Puts unused program fragments into the virtual memory. When you need to use them, load them into the primary memory (physical memory. This is what memory management needs to do. Another thing needs to be done for memory management: the physical location of the computing program fragment in the primary memory for CPU scheduling.

Memory Management includes block-Based Management, page-based management, and segment-based page management. Common segment Page Management

Block management: divides the primary storage into one large block and one large block. When the required program fragments are not in the primary storage, a primary storage space is allocated and the Sequential parts are loaded into the primary storage, even if only a few bytes are needed, the program can only be allocated to it. This will cause a lot of waste, with an average waste of 50% of the memory space, but it is easy to manage.

Page Management: The primary storage is divided into one page and one page. The space on each page is much smaller than one page. Obviously, the space utilization of this method is much higher than that of block management.

Segment management: the primary storage is divided into segments. The space of each segment is much smaller than that of one page. This method is much higher than page management in terms of space utilization, however, it also has another disadvantage. A program segment may be divided into dozens of segments, so much time will be wasted on computing the physical address of each segment (I/O is the most time-consuming computer ).

Segment-and-Page Management: combines the advantages of segment-And page-based management. The primary storage is divided into several pages, each of which is divided into several sections. The benefits are obvious. I don't need to say more.

Various memory management methods are used to calculate the physical addresses of program fragments in the primary memory. In fact, they are similar.

This is just a rough idea. It is not enough to explain the knowledge of memory management. No matter which operating system book has a detailed explanation

--------------------------------------------------------------------------
4.
# Include "stdafx. H"
# Define sqr (x) x * x

Int main (INT argc, char * argv [])
{
Int A = 10;
Int K = 2;
Int M = 1;

A/= sqr (K + M)/sqr (K + M );
Printf ("% d \ n", );

Return 0;
}
What is the result of this question?

Define is just a definition. in programming, it is just a simple replacement of x * X. It does not go through arithmetic rules.

A/= (k + M) * (K + M)/(K + M) * (K + M );
=> A/= (k + M) * 1 * (K + M );
=> A = A/9;
=> A = 1;

PS: after self-verification, although the results are the same, it should not be a/= K + M * k + M/K + M * k + m;

Because the value of sqr (K + M)/sqr (K + M) is 7, not 9.

--------------------------------------------------------------------------
5.
Const symbol constant;
(1) const char * P
(2) Char const * P
(3) char * const P
The differences described above are described;

If the const is on the left side of the asterisk, the const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant;
If const is on the right side of the asterisk, const modifies the pointer itself, that is, the pointer itself is a constant.

(1) const char * P

A const object pointer to the char type. P is not a constant. We can modify the value of P to point to a different char, but it cannot be changed to a non-Char object, for example:
Const char * P;
Char C1 = 'a ';
Char C2 = 'B ';
P = & C1; // OK
P = & C2; // OK
* P = C1; // Error

(2) Char const * P
(3) char * const P

The two seem the same. In this case, * P can be modified, but P cannot be modified.

(4) const char * const P
The address and the target object cannot be modified.

--------------------------------------------------------------------------
6. below are two if statement judgment methods in C language. Which of the following statements is better? Why?
Int N;
If (n = 10) // the first judgment method
If (10 = N) // method 2

If a = sign is missing, an error will be reported during compilation, which reduces the number of rows that may cause an error and can be detected if = is missing.

--------------------------------------------------------------------------
7. What is the problem with the following code?
Void dosomething (...)
{
Char * P;
...
P = malloc (1024); // allocate 1 K space
If (null = P)
Return;
...
P = realloc (p, 2048); // The space is not enough and is allocated to 2 k again.
If (null = P)
Return;
...
}

A:
P = malloc (1024); It should be written as: P = (char *) malloc (1024 );
No space for P is released, causing memory leakage.

--------------------------------------------------------------------------
8. What is the problem with the following code? Provide the correct statement.
Void dosomething (char * P)
{
Char STR [16];
Int N;
Assert (null! = P );
Sscanf (P, "% S % d", STR, N );
If (0 = strcmp (STR, "something "))
{
...
}
}

A:
Sscanf (P, "% S % d", STR, n); this sentence is written as: sscanf (P, "% S % d", STR, & N );

--------------------------------------------------------------------------
9. What are the following code errors?
Void test1 ()
{
Char string [10];
Char * str1 = "0123456789 ";
Strcpy (string, str1 );
}

Array out-of-bounds

--------------------------------------------------------------------------
10. What is the problem with the following code?
Void Test2 ()
{
Char string [10], str1 [10];
For (I = 0; I <10; I ++)
{
Str1 [I] = 'a ';
}
Strcpy (string, str1 );
}

Array out-of-bounds

--------------------------------------------------------------------------
11. What is the problem with the following code?
Void test3 (char * str1)
{
Char string [10];
If (strlen (str1) <= 10)
{
Strcpy (string, str1 );
}
}

= Array out-of-bounds
= The End mark of strcpy copy is to find the \ 0 in the string. Therefore, if \ 0 is not encountered in the string, it will be copied until \ 0 is encountered, and the above 123 will be out of bounds.
 
We recommend that you use strncpy and memcpy.

--------------------------------------------------------------------------
12. What is the problem with the following code?

# Define max_srm 256

DSN get_srm_no ()
{
Static int srm_no; // isn't the initial value assigned here?
Int I;
For (I = 0; I <max_srm; I ++, srm_no ++)
{
Srm_no % = max_srm;
If (my_srm.state = idle)
{
Break;
}
}
If (I> = max_srm)
Return (null_srm );
Else
Return srm_no;
}

The system initializes the static int variable to 0, but the value is always saved. The so-called non-reentrant...

--------------------------------------------------------------------------
13. Write the running result:
{// Test1
Char STR [] = "world"; cout <sizeof (STR) <":";
Char * P = STR; cout <sizeof (p) <":";
Char I = 10; cout <sizeof (I) <":";
Void * PP = malloc (10); cout <sizeof (p) <Endl;
}

6: 4: 1: 4

--------------------------------------------------------------------------
14. Write the running result:
{// Test2
Union v {
Struct X {
Unsigned char S1: 2;
Unsigned char S2: 3;
Unsigned char S3: 3;
} X;

Unsigned char C;
} V;

V. C = 100;
Printf ("% d", V. X. S3 );

}

3

--------------------------------------------------------------------------
15. How can I determine whether an operating system is a 16-bit or 32-bit program written in C ++? The sizeof () function cannot be used.

A1:
In a 16-bit system,
Int I = 65536;
Cout <I; // output 0;
Int I = 65535;
Cout <I; // output-1;

In a 32-bit system,
Int I = 65536;
Cout <I; // output 65536;
Int I = 65535;
Cout <I; // output 65535;

A2:

Int A = ~ 0;
If (A> 65536)
{
Cout <"32 bit" <Endl;
}
Else
{
Cout <"16 bit" <Endl;
}

--------------------------------------------------------------------------
16. What is the difference between C ++ and C ++?

Mechanism: C is process-oriented (but C can also write Object-oriented Programs); C ++ is object-oriented and provides classes. However,
C ++ is easier to write object-oriented programs than C

From the applicable direction: C is suitable for scenarios that require small code size and high efficiency, such as embedded; C ++ is suitable for upper-level and complex; llinux is mostly written in C, because it is system software, the efficiency requirement is extremely high.

From the name, we can see that C ++ is more + than C, which means C ++ is the superset of C. Why is C ++ not called C ++, because of the C ++ Ratio
C, there are too many extended things, so I put two + s behind C, so it became C ++

C is a structured programming language, and C ++ is an object-oriented programming language.
C ++ focuses on objects rather than processes, and on class design rather than logic design.

--------------------------------------------------------------------------
17. Switch the values of two parameters without using third-party parameters.
# Include <stdio. h>

Void main ()
{
Int I = 60;
Int J = 50;
I = I + J;
J = I-j;
I = I-j;
Printf ("I = % d \ n", I );
Printf ("J = % d \ n", J );
}

Method 2:
I ^ = J;
J ^ = I;
I ^ = J;

Method 3:
// Use addition and subtraction without Overflow
A = a + B-(B =)

--------------------------------------------------------------------------
18. Questions about bit domains (why is the output a strange character)

A. t = 'B'; the effect is equivalent to a. t = 'B' & 0xf;

'B' --> 01100010
'B' & 0xf -- & gt; 00000010
Therefore, the output ASCII code is a special character of 2.

Char T: 4; is a 4-bit character variable, the same
Unsigned short I: 8; is the 8-bit unsigned short integer variable.

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.