# Programmer interview question Selection

Source: Internet
Author: User
Tags bitset

The questions come from the programmer's interview guide and some difficult points I encountered during the interview test.

++ Stack function parameters
Int array [] = {1, 2, 3, 4, 5}
Int * PTR = array;
* (PTR ++) + = 123;
Printf ("% d, % d", * PTR, * (++ PTR ));
Result: 3

Float I = 1.0f
Cout <(INT) I <Endl; // output 1
Cout <& I <Endl; // output address of I
Cout <(Int &) I <Endl; // output first 32 bit value of float I's memory address 1065353216
Cout <(INT) A = (Int &) a) <Endl; // output false

Type conversion and pointer
Unsigned int A = 0xfffffff7;
Unsigned char I = (unsigned char) A; // I is the first eight digits of a truncated
Char * P = (char *) & A; // P is a pointer to the & A type.
Printf ("% 08x, % 08x/N", I, * P); // cout 000000f7, fffffff7

Int main (INT argc, char * argv []) <br/>{</P> <p> int A   =, 4, 5 }; <br/> int ** P; <br/> P = (INT **) A; <br/> cout <* (p + 1) <Endl; // 0x00000008 <br/> return 0; <br/>}</P> <p>

If the address of array a is X, p + 1 = x + sizeof (int *) = x + 4, (p + 1) points to 8, and (p + 1) 8 is obtained for the example of unreferenced, but p is the pointer to the pointer. Therefore, the output is 0x00000008 in the address format.

And or non-Applications
Use an expression to determine whether X is a power of 2: (x> 0 )&&! (X & (x-1 ))

X is odd return (X & 1) = 1
Evaluate the binary notation of a number, M = \$1; while (m) {CNT ++; M = M & (S-1) ;}return CNT;
Exchange a B: A = a ^ B; B = a ^ B; A = a ^ B;

A = a + B-(B =)

Return (A + B)/2 + ABS (a-B)/2

Sizeof
Struct {long a; char B;} A; // sizeof (A) = 8;
Char * P = "0123456789"; // sizeof (p) = 4
Char STR [] = "0123456"; // sizeof (STR) = 7
Char Q [] = "A/N"; // sizeof (q) = 3
Bool val; // sizeof (VAL) = 1
// Sizeof (float) = 4
// Sizeof (double) = 8
Int fun () {...} // sizeof (fun) = 4
// Sizeof (string) = 4
String Array  // sizeof (array) = 16
Class A {}; // sizeof (A) = 1; see C ++ OBJ modeule

The advantage of inline over macro is that the former performs type checks.

Pointer and reference
The pointer can be null and the reference must be initialized to a certain object during declaration. Therefore, the validity check is not required for the reference.
The reference cannot be modified once it is initialized. Note that the content of the referenced object can be modified. Therefore, when the const limit is added, there is only one situation, that is, the const reference, which refers to

. Int & Const P = A is Int & P = A; and const Int & P = A indicates const int.
When the pointer is added to the const limit, there are two kinds of meanings. Int const * P = & A indicates that the content pointed to by the pointer cannot be modified, int * const P = & A indicates that the pointer itself cannot point to other objects. After the combination, const int * const P = & A also limits that both cannot be modified.

Participate pointer
Int A ;
Fun (){
Cout <sizeof (a); // 4
}

Void getmem (char * P, int N)
{
P = (char *) malloc (n); // The parameter P cannot modify the STR value.
// Return P;
}

Int main ()
{
Char * STR = NULL;
Getmem (STR, 10); // STR = NULL
// STR = getmem (10)
Strcpy (STR, "hello ");
Free (STR );
}

String constant pointer
Const char * Fun ()
{
Char STR [] = "Hello World"; // wrong STR in stack
// Char * STR = "Hello World"; // right STR in global store Zone
Return STR;
}

Functions, pointers, and Arrays
Int * A  // array a , each element is an int pointer
Int ** A   // sizeof (A) = 48 A is array a [I] [J] is a PTR of PTR
INT (* A)  // A is a pointer to a one-dimensional array. The array element is int type.
INT (** A)  // A is a second-level pointer, which points to a pointer pointing to a given array.
Int * (* A)  // A is a pointer to a one-dimensional array, and the array element is int * type.
INT (* A ) () // A is a one-dimensional array. Each element is a function pointer whose return value is int.
Int * (* A) ) // same as int * (* A) 
INT (* A) (INT) // A is a function pointer pointing to a function with an int parameter.
INT (* f) (INT, INT) (INT) // F is the function pointer, pointing to a function fun (INT, INT ), the fun return value is also a function pointer, And the type is int fun (INT );

Malloc/free and new/delete
The former is a library function and only applies for space. The latter is an operator that performs class-type operations. The latter performs construction and destructor operations.

Pointer and handle
The handle points to the address of an object, which is changed due to the shift of the object to the memory management operation.

Smart pointer
Intelligence can automatically analyze the structure of the object after it leaves the function, and parse the object when an exception is thrown to prevent memory leakage.

Class member variables
Static member variables of a class can be public or private. Static members of a class can only be initialized outside the class. T class: s_mem = 0.
If Initialization is not performed, compilation fails. Static member variables and functions of the class only have file scopes.

Class constant members can only be initialized in the constructor initialization list.

Overloading means that a function has different parameters or restrictions. Overloading is done statically by the compiler and does not need to be completed dynamically.
Rewriting refers to the re-Implementation of the virtual functions of the base class in the derived class. The parameters and return values of the function are not changed. Rewriting is implemented by using virtual functions.

Access Control
Public private protected
When acting on a class member, the Public Member indicates that it is visible to the outside of the class, the private Member indicates that the member is invisible to the outside of the class, and can only be used inside the class, and is not visible to the subclass,

The protected member is invisible to the outside of the class, but visible to the subclass.
When modifying a class in the inheritance system, public inheritance indicates that the subclass can access non-private members in the parent class,

You can also access non-Private Members of the base class through subclass objects. Private inheritance

Indicates that the subclass object cannot access all the members of the base class, but can access non-Private Members of the base class in the subclass. Protected inheritance is similar to private inheritance. In the subclass, you can access the base class.

The subclass object can access non-Private Members of the base class. These members are inherited and become protected, and cannot be accessed by subclass.
Private inheritance and protect inheritance are only useful for discussion and are not discussed in software design.
If no keyword is used in inheritance, private inheritance is used by default.

Bit operations and embedded
Int main ()
{
Int A = 5; Double B = 5.01;
Printf ("% F/N", a); // XXXXX
Printf ("% d/N", B); // 1889785610
}
For the printf function, when % F is output, the parameter should be of the double type. If the parameter is not of the double type, the 8B Interpretation will be read from the stack. For % d, if the parameter is double

Only the first four digits are explained.

Set a method
# Define bitn (0x1 <n)
Void set_bitn (int * P)
{
* P = * p | bitn;
}

Void clr_bitn (int * P)
{
* P & = ~ Bitn
}

The interrupt service program (ISR) requires no parameters, no return values, no floating point computing, and no re-entry problems such as printf.

Int * PTR;
PTR = (int *) 0x679a;
* PTR = 1;

Malloc (0) returns a valid non-null pointer.

Method for Determining the CPU size
Int x = 1;
If (* (char *) & X = 1)
// Little endian
Else
// Big endian

Bit field assignment

# Include "stdio. H "<br/> # include" stdlib. H "<br/> # include <bitset> <br/> # include <iostream> <br/> using namespace STD; <br/> Union v {<br/> struct X {<br/> unsigned char S1: 2; <br/> unsigned char S2: 3; <br/> unsigned char S3: 3; <br/>}x; <br/> unsigned char C; <br/>}v; <br/> int <br/> main () <br/> {<br/> v. C = 100; <br/> printf ("% d/N", V. x. s1, V. x. s2, V. x. s3); <br/> bitset <8> uchar (100); <br/> cout <uchar <Endl; </P> <p>}

The result is 01100100.

0 1 3

That is to say, the bit fields in a single byte are divided from low to high. In this way, S1 uses the 00 with two low bits, S2 uses the 001 With two digits in the middle, and S3 uses the 011 with two bits in height.

Variable Memory Allocation
The process is divided into three parts in the memory: code segment, static data zone, and dynamic data zone.
A Dynamic Data zone generally refers to a stack that stores automatic variables. A static data zone stores global and static variables in another region.

Memory Allocation Function new
For memory allocation of basic data types, the operating system has recorded the size of memory space allocated, and for memory allocation of class types with destructor, the number of calls to the Destructor is also recorded, so an additional 4 bytes are used to record the size of the array. In addition, in windows, there are four bytes after the new memory as the memory allocation boundary.

Inter-process communication methods: signal, semaphore, message queue, and shared memory

Raid Classification
More than 0 disks form a large hard disk, which can be operated in parallel without redundancy. The disk usage is N.
1. operate both the primary disk and the image disk. The lowest disk utilization rate is 1/2.
3. Use a separate disk to store the verification data of other disks. When an error occurs on other disks, the data is restored through verification. However, if an error occurs on the verification disk, the data of other disks cannot be used and the disk utilization is n-1.
5. Check disks are distributed across disks. Highest security, with the disk utilization of N-1
0-1 parallel disk operations, and half of the disk as an image at the same time.

Related Keywords:

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.