Written tests common function questions (reading these functions, the written test is a lot stronger)

Source: Internet
Author: User

1:

Judging if there is no sign

void foo (voidint6; int b =-;(a6)? Puts ("> 6"): Puts ("<= 6");}

Answers and explanations:

Output > 6, because all operands are automatically converted to unsigned types when there is a signed type and an unsigned type in an expression. -20 A very large positive number.

Add:

To determine a macro function that has no symbols:
#define ISSIGNAL (x) ((x>=0 && ~x>=0)? 1:0)//1 is unsigned 0 signed

2:

int 0  while++ = x& (x1return

Answers and explanations:

Assume x = 9999. Answer: 8
Idea: Convert x into 2, and see the number of 1.

3:

#include <stdio.h>void  myprint (x) {    int i=0;      for (i=; i>=0; i--) {        printf ("%d", (x>>i ) &1);        }    } void Main () {    myprint (5);    }

Function: Outputs a number in binary

4:

! Note Macro definition mechanical substitution
If there is a macro definition: #define MOD (x, y) x%y

The output after executing the following statement is

int a=13,b=94;

printf (″%d\n″,mod (b,a+4));

A.5

B.7

C.9

d.11

Reference Answer: B

But:
#define N 3

#define Y (N) ((n+1) *n)//Difference # define Y (N) ((n+1) * (n))!

The value of the expression-n+y (5+1) is

a.42

b.48

c.54

D. Error


Reference Answer: B
Y (5+1) Pass the past should be 6, rather than simply replace the 5+1

5:

Main () {    int a[5]={1,2,3,4,5};      int *ptr= (int *) (&a+1);      printf ("%d,%d", * (A +1), * (ptr-1));}

Answers and explanations:

Analysis: * (a+1) is a[1],* (ptr-1) is a[4], the execution result is 2, 5. &a+1 is not the first address +1, the system will assume that an array of a is offset,
is offset by the size of an array (this example is 5 int) int *ptr= (int *) (&a+1); The PTR is actually & (A[5]), which is a+5 for the following reasons:
&a is an array pointer whose type is int (*) [5];
and the pointer plus 1 to be based on the pointer type plus a certain value, different types of pointer +1 increase the size of the different
A is an int array pointer of length 5, so add 5*sizeof (int) so PTR is actually a[5]//a[5] The address of the next array object = = Element First Address
However, PRT is not the same as (&a+1) type (this is important)
So prt-1 will only subtract sizeof (int*). The address of the A,&a is the same, but the meaning is not the same, a is the address of the first address of the array, that is a[0], &a is the object (
Array) First address, a+1 is the address of the next element of the array, that is, A[1],&a+1 is the first address of the next object, that is, a[5].

6:

CharStr1[] ="ABC"; CharStr2[] ="ABC"; Const CharStr3[] ="ABC"; Const CharStr4[] ="ABC"; Const Char*STR5 ="ABC"; Const Char*STR6 ="ABC"; Char*STR7 ="ABC"; Char*STR8 ="ABC"; cout< < (str1 = = str2) < <Endl;cout< < (STR3 = = STR4) < <Endl; cout< < (STR5 = = STR6) < <Endl; cout< < (STR7 = = Str8) < < Endl;

Answers and explanations:

The result is: 0 0 1 1
STR1,STR2,STR3,STR4 are array variables, they have their own memory space, pointing to different memory, STR1 is equivalent to a pointer variable with independent memory space;
Whereas str5,str6,str7,str8 are pointers, they point to the same constant area.

7:

int   Main ()  {         char  A;              Char *str=&A;      Char str1[];    strcpy (str,"hello");  (wrong)    strcpy (str1,"hello");(to)        printf (str);            return 0 ;  }   

Answers and explanations:

There is no memory space allocated for STR, an exception will occur, the problem is to copy a string into a word
The address indicated by the pointer to the symbol variable. Although the results can be output correctly, the program is caused by a memory read and write
Collapse.

8:

Initialization of member data using initialization lists and assignment methods

Class a{
int A, B;
Public
A (int x): b (x), a (b) {}
};
The initialization list is executed in the order in which the members are defined, that is, although you write a in the list, but because a is defined first,
So the initialization of a is performed first. That is, your A is initialized with the value of uninitialized B.
Class Cexample {
Public
int A; float B;
Cexample (): A (0), B (8.8) {}//constructor initialization list
Cexample () {a=0; b=8.8;}
};
Constructor internal Assignment Cexample () {a=0; b=8.8;}}; The result of the two C + + constructors in the above example is
The same. The above constructor (using the constructor of the initialization list) explicitly initializes the members of the class, without using initialization
The constructor of the list is a member of the class (so the method is not available when the member data is const, it must be initialized)! ,
There is no explicit initialization.

9:

Char *p1;      Long *p2;      P1Char *)0x801000;     P2Long *)0x810000;     Excuse me p1+5=  ;              p2+5=  ;  

Answer: 801005; 810014. Do not forget that this is a 16 binary number, P2 to add 20 to 16 binary is 14

10:

Summary of common operational order issues between pointers and * and + +

intI=1;p rintf ("%d%d", i++,i++);//2 1printf"%d", i);//3intarr[]={6,7,8,9,Ten}; int*ptr=arr; * (ptr++) + =123;//First Count *ptr=*ptr+123, then ptr++; so arr[0]= 129; *p++=* (p++)?printf ("%d,%d", *ptr,* (++ptr));//right-to-left orderinta[3]; a[0]=0; a[1]=1; a[2]=2; int*p, *Q; P=A; Q=&a[2]; Then A[q-p]=?Answer: A[q-p]=a[2]=2The problem is to tell us the operation characteristics of the pointer???


11:

typedef Union  {  long  i;   int k[5];   Char C;} DATE;   struct Data  {       int  cat;       DATE Cow;        Double Dog;} too;   DATE Max;   Then the statement printf ("%d",sizeof(too) +sizeof(max)), the execution result is:  the

12:

Static variables are in the global static zone, and the next access retains the last assignment

intSuminta) {autointC=0; Staticintb=3; C+=1; b+=2; return(a+b+c); }   voidMain () {intI;intA=2;  for(i=0; I <5; i++) {printf ("%d,", SUM (a)); }  } 

Answer: 8,10,12,14,16

Written tests common function questions (reading these functions, the written test is a lot stronger)

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.