A guide to interviewing C + + programmers

Source: Internet
Author: User
Tags define local modifiers reserved strlen volatile
A guide to interviewing C + + programmers

Interview Question 2: What is the difference between the declaration and definition of a variable?

A: Assign addresses and storage space to variables called definitions, and do not allocate address memory space called declarations. A variable can be declared in multiple places, but only in one place. An extern modifier is a declaration of a variable, stating that the variable will be defined outside the file or later in the file.

Interview Question 3: How to make the computer buzzer sound in the simplest way.

Answer: #include <stdio.h>

int main (int argc, char *argv[])

{

Printf ("\7");

return 0;

}

Interview Question 4: Talk about your understanding or understanding of programming specifications.

A: The programming specification can be summed up as: program feasibility, readability, portability and testability.

Interview Question 5: What are the rules for naming functions, variables, and so on?

A: It contains numbers, ASCII letters, underscores, starts with letters or underscores, and cannot be used as variable names with reserved words, but also with high readability.

Interview Question 6: Write out bool,int,float, pointer variable and "0 value" comparison of If statement.

Answer: BOOL type data:

IF (flag)

{

A

}

Else

{

B

}

INT type data:

If (0!=flag)

{

A

}

Else

{

B

}

Pointer type data:

If (Null==flag)

{

A

}

Else

{

B

}

Float type data:

Const float norm=0.00001;

If ((flag>=norm) && (flag<=norm)

{

A

}

Interview question 7:short i=0;i=i+1l; Are these two sentences wrong?

Are the following two pieces of code correct?

Code One:

Short s1=1;s1=s1+1l;

Code two:

Short s1=1;s1+=1l;

A: The code one is wrong, code two is correct.

In "s1+1l", because L is a long integer constant, S1 is converted to a long integer and then added to 1, and the result is a long integer. In "s1=s1+1l", a long integer result is assigned to the short type S1, that is, to implicitly convert a large data to a small type, the compilation will certainly be wrong. You need to force type conversions in the following form:

S1= (short) (s1+1l);

The "+ +" operator in the second paragraph of code has already performed a strong type, so there is no error.

Interview questions 8:char x[]={"ABCD" and Char y[]={' a ', ' B ', ' C ', ' d '} are different.

Answer: different. Because the char x[]={"ABCD"} This writes, the string ABCD including the terminator is stored to x. and Char y[]={' a ', ' B ', ' C ', ' d '} This is storing 4 characters a,b,c,d to array y.

Interview questions 9:char type data is stored in memory form.

Answer: ASCII code

(Any data is stored in binary form in memory, and the numeric value is expressed as a complement.) The complement of a positive number is the same as the original code and the original code. The complement method of negative numbers is to put the binary form of its absolute value in "bitwise negation plus 1".

Interview questions 10:&& and &,| | and | What's the difference.

A: (1) & and | Calculation of operands,&& and | | Just judge the logical relationship.

(2) && | | The right-hand operand is no longer evaluated when the left-hand operand is judged to determine the result.

Interview Question 11: What is the left value, what is the right point.

A: Right value: Data stored in a memory address. Data that is also called a variable.

Left value: The address of the memory that stores the data value. Also known as the address value of a variable.

(The left value can be the right value, but the right value may not be the left value)

The difference between the interview question 12:sizeof and strlen

Answer: sizeof and strlen have the following differences:

(1) sizeof is an operator, strlen is a library function.

(2) The sizeof parameter can be either a data type or a variable, and the strlen can only make arguments with the string at the end '.

(3) The compiler calculates the sizeof result at compile time, and the strlen function must be computed at run time, and sizeof calculates the size of the data type as the memory, and strlen the actual length of the string being computed.

(4) The array does sizeof parameter not degenerate, pass to strlen to degenerate to the pointer.

(5) The result types of sizeof and strlen are size_t.

Interview Question 13: The structure can be directly assigned value.

A: The declaration can be directly initialized, the same structure of different objects can also be directly assigned to the value, but when the structure contains pointers to "members" must be careful. (when more than one pointer points to the same memory, the release of this memory by a pointer may result in illegal operation of other pointers.) So make sure that the other pointers no longer use this memory space before releasing them. )

Interview question 14: The structure of the body to maintain alignment problem.

A: A struct is a composite data type, and its constituent elements can be either a variable of a basic data type or a combination of some types of data. In this case, the compiler automatically aligns member variables to increase the rate of operation. By default, space is allocated according to the natural alignment criteria, and each member is stored sequentially in the order in which they are declared, with the address of the first member the same as the address of the entire structure, aligned to the largest member of the structure body member.

The definition of a bit field is similar to that of a struct body, in the form of:

STRUCT the structure name of the bit field

{

bit field List

};

The list of bit fields is in the form of:

Type descriptor bit domain name: length of Bit field

For example:

Struct BS

{

Int A:8;

Int B:2;

Int C:6;

};

For the definition of a bit field, there are several explanations:

(1) A bit field must be stored in the same byte and cannot span two bytes. If there is not enough space left for one byte to hold another domain, the bit field should be stored from the unit. You can also intentionally make a bit field start with the next cell.

(2) Because the bit field is not allowed to span two bytes. Therefore, the length of a bit field cannot be greater than the length of one byte. That is, no more than 8-bit binary.

(3) A bit field can have a domain name, which is used only to populate or adjust the location. Unknown bit fields are not available.

Interview question 15: keyword static in C and C + + differences.

A: Static in C is used to modify local static variables and external static variables, functions. In addition to the above functions in C + +, it is also used to define the member variables and functions of the class, that is, static members and static member functions.

Interview questions 16:C language structure and C + + what is the difference. (C language Structure and C + + class What is the difference)

A: C language structure and C + + structure are mainly the following differences:

(1) C language structure cannot have function member, and C + + class can have.

(2) The data members in the C language structure are not restricted by private, public and protected access. The class members of C + + have these access restrictions (the members of the struct in C + + have access rights set, but the default access property of the class member is private, and the default Access property of the struct is public.)

(3) C language structure is not inherited, but C + + class has a rich inheritance relationship.

What is the difference between the malloc in the interview question 17:c and the new in C + +?

A: malloc and new have the following differences:

(1) New, delete is an operator that can be overloaded and can only be used in C + +.

(2) malloc, free is a function, you can overwrite, C, C + + can be used.

(3) New can call the constructor of the object, and the corresponding delete invokes the corresponding destructor.

(4) malloc only allocates memory, free only reclaims memory. Constructors and destructors are not executed.

(5) New,delete returns some kind of data type pointer, malloc, FREE returns a void pointer.

What is the difference between the 18:c++ of the interview question and the C-language pointer?

A: Pointers and references have the following main differences:

(1) The reference must be initialized, but the storage space is not allocated, the pointer is uninitialized when it is declared, and the storage space needs to be allocated when initializing.

(2) The reference cannot be changed after initialization, and the pointer can change the object being referred to.

(3) There is no reference to a null value. However, there is a pointer to a null value.

Fourth Chapter pretreatment reserved words

Preprocessing refers to the work done before the first scan (lexical scan and parsing) of the compilation. Preprocessing is an important functional feature of C and C + + languages. It is done by the preprocessor. When compiling a source file, the system will automatically reference the preprocessor to process the preprocessing part of the source program, and automatically enter the source program's compilation after processing.

Interview Question 19: Briefly describe the role of #ifdef, #endif, #else和 #ifndef.

A: These conditional compilation directives mainly have the following functions:

(1) The use of #ifdef, #endif可将某程序功能模块包括进去 to provide functionality to specific users.

(2) is used to mark before the subroutine, so as to facilitate tracking and mode.

(3) The hardware should be limited.

Interview question 20: What is the difference between a macro definition and a function?

For:

(1) A reference to a macro takes up only the compile time, not the elapsed time

(2) The reference to the macro does not return a value, and if necessary, enclose the entire expression.

(3) The macro has no type of formal parameter. The formal parameters of a function must have a type.

(4) The case where the argument is an expression. If you do not enclose the expression in parentheses, you may cause incorrect results, and the function will not

(5) Direct substitution of macros may cause side effects, and functions will not.

Interview question 21: Write a "standard" macro min. (Write a "standard" macro min, this macro enters two parameters and returns the smaller one)

A: #define MIN (A,b) ((a) >= (b)? ( A):(B))

What is the difference between the interview question 22:typedef and define?

For:

(1) Different usage: the alias that a typedef uses to define a data type. Enhances the readability of the program. Define is primarily used to define constants. As well as writing a complex, frequently used macro.

(2) Different execution time: TypeDef is part of the compilation process, with the function of type checking, define is the macro definition, is the precompiled part, it occurs before compiling, but simply replaces the string. Do not check for type

(3) Different scopes: the TypeDef has scope limitations. Define is not scoped. As long as the reference after the Define declaration is correct.

(4) The operation of the pointer is different: there are great differences between the pointers defined by the TypeDef and the define.

(5) A TypeDef definition is a statement, because the end of a sentence is a semicolon, and define is not a statement and cannot be appended with a semicolon.

Interview Title: #define Char char* and typedef char * char each have advantages.

A: A type alias defined by define can be extended by other modifiers (such as: unsigned), and a typedef is not available. When a define-defined type alias represents a pointer, only the first of its successive declared variables is a pointer. All others are ordinary variables that are not pointers, and a typedef can guarantee that all variables declared continuously are of the same type.

Interview Question 24: Talk about your understanding of the TypeDef.

A: There are four different uses of typedef:

(1) Define a type of Alias

(2) used in the old C code to assist the declaration struct

(3) Define platform-independent types

(4) Define a new simple alias for complex declarations

There are two other things to note:

(1) A new alias of a type is defined and cannot be extended by other modifiers.

(2) A typedef is a storage-class keyword that does not really affect the storage characteristics of an object.

Interview Question 25: What is the keyword const?

A: const is used to define a read-only variable or object. Main advantages:

(1) Easy to type check, the same as macro definition can easily modify and adjust the parameters.

(2) Save space and avoid unnecessary memory allocation

(3) Provide reference for function overloading

Interview questions 26:const, define define the difference between constants. (What is the effect of constants?) Both const and define can be used to define constants, both of which are different. )

A: The introduction of constants can enhance the readability of the program. Can make the program maintenance and mode more convenient, make writing simple

The difference between the two is as follows:

(1) A const-defined constant has a data type, and define does not

(2) Many integrated development environments only support the mode of constants defined by const, and do not support define defined constants

(3) The constants defined by const are to allocate the memory space, while the constants defined by define do not allocate space, so the const-defined constants have more advantages than the constants defined by define, which can be said to be the optimization of define.

The interview question 27:static what function.

A: In C, static mainly defines a global static variable, defines a local static variable, and defines a static function

(1) Define a global static variable: A global variable preceded by a keyword static, which becomes a global static variable. Global static variables have the following characteristics:

(1) Allocating memory within the global data area

(2) If not initialized, the default value is 0

(3) The variable is visible from the definition to the end of the file within this document

(2) Define local static variables: The local static variable is preceded by the keyword static, and the local variable becomes a static local variable. Static local variables have the following characteristics:

(1) The variable allocates memory in the global data area

(2) If initialization is not displayed, it is implicitly initialized to 0

(3) It always resides in the global data area until the program has finished running

(4) Its scope is a local scope, and when the function or statement block that defines it ends, its scope ends.

(3) Define a static function: When the return type of a function is added to the static keyword, the function is defined as a static function. Static functions have the following characteristics:

(1) Static functions can only be used in source files

(2) The inline function declared in the file scope defaults to static

There are two new roles in C + +: Defining static data members or static function members

(1) define static data members. Static data members have the following characteristics:

(1) Memory allocation: Allocating in the program's global data area

(2) initialization and definition: a static data member is defined to allocate space, so it cannot be defined in the class declaration

(3) static member functions. A static member function is associated with a class and is not associated with a class object. Static member functions cannot access non-static data members. The reason is simple, non-static data members belong to a particular class instance and are used primarily for operations on static data members.

The interview question 29:extern what function.

A. An extern identity variable or function declaration is defined in another file that prompts the compiler to find its definition in other modules when it encounters variables and functions

Interview Question 30: How to avoid wild pointers.

Answer: "Wild pointer" causes and solutions are as follows:

(1) The pointer variable declaration was not initialized. Workaround: When a pointer is declared, it can be a specific address value, or it can point to null.

(2) The pointer p is not set to null after being free or delete. Workaround: The pointer application points to NULL when the memory space that the pointer points to is freed.

(3) The pointer operation goes beyond the scope of the variable. Workaround: Release the address space of the variable and let the pointer point to NULL before the scope of the variable ends.

What is the difference between the interview question 31:a and &a?

A: Array name A can be the first address of an array, and &a is a pointer to an array.

Interview Question 32: Briefly describe the difference between a pointer constant and a constant pointer.

A: Pointer constants are defined as a pointer, the value of this pointer can only be initialized in the definition, other places can not be changed, the constant pointer is defined a pointer to a read-only object, the constant pointer can not change the value of the object.

The pointer constant emphasizes the immutable nature of the pointer, while the constant pointer emphasizes the immutable nature of the object to be referred to.

Interview Question 33: Why does the stream operator overload return a reference.

A: In the program, stream operators >> and << are often used continuously. So the return value of these two operators should be a stream reference that still supports these two operators. None of the other data types can do this.

Interview Question 34: What is the role of frequent references?

A: frequently referenced references are intended to avoid changing the value of a variable without knowing it by using a reference to a variable. An alias that is used primarily to define a read-only property of a generic variable as the descendant parameter of a function. Prevents the argument from being accidentally changed in the calling function.

Interview Question 35: What is a reference.

A: A reference is an alias for a target variable, which is the same for all references and direct manipulation of variables, mainly for function parameters, function return values, and constant references.

The interview question 36:volatile what function.

A: Volatile has the following effects:

(1) State registers a class of parallel device hardware registers

(2) A non-automatic variable that is accessed by an interrupt service subroutine

(3) The variables shared by several tasks among multiple threads

Interview Question 37: A parameter can be both const and volatile.

A: You can use const and volatile to modify variables at the same time, indicating that the variable is read-only within the program, can not be changed, only changes in the external conditions of the program, and the compiler will not optimize the variable. Each time you use this variable, be careful to read the value of the variable in memory,

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.