(Various companies interview original questions) online has done a set of CC ++ integrated test questions, also to test your level (2), cc integrated test questions

Source: Internet
Author: User

(Various companies interview original questions) online has done a set of CC ++ integrated test questions, also to test your level (2), cc integrated test questions

I have read the last 10 questions and sent them to me.

 

The following is a question, and I have some understanding of the question.


 

Top 10 question addresses

(Various companies interview original questions) online has done a set of CC ++ comprehensive test questions, also to test your level (1)

 

11. The class A, B, C, and D4 have been defined. In the program, the calling sequence of the Destructor A, B, C, and D is?

C c;  void main()  {       A*pa=new A();       B b;       static D d;       delete pa;  }  


A. A B C D

B. A B D C

C. A C D B

D. A C B D

 



Answer: B

C is a global variable, a and B are local variables, and d is a static local variable.

After the first construction, the static local variable d is destructed at the end of the main function, and the global variable c is destructed at the end of the program.

 

Statement

delete pa;  

Make a analyze the structure prior to B

 

Diagram




12.

If char is a byte, int Is 4 bytes, and pointer type is 4 bytes, the Code is as follows:

class CTest  {  public:  CTest():m_chData(‘\0’),m_nData(0)  {  }  virtual void mem_fun(){}  private:  char m_chData;  int m_nData;  static char s_chData;  };  char CTest::s_chData=’\0’;  


Q:

(1) What is the value of sizeof (CTest) aligned in 4 bytes?

(2) What is the value of sizeof (CTest) aligned in 1 byte?

Select the correct answer.

A. 16 4

B. 16 10

C. 12 9

D. 10 10

 



Answer: C

First, the variables and functions that occupy the storage space in the analysis class CTest

Variable: the char variable occupies 1 byte, the int variable occupies 4 bytes, And the length obtained by sizeof (CTest) does not include the number of bytes of the static variable.

Function: common functions of the class are allocated memory space during instantiation. virtual functions (no matter how many) maintain a virtual function table pointed to by a pointer, which occupies 4 bytes.

So,

4 (int) + 4 (char with a length of 1 byte) + 4 (pointer to the virtual function table) = 12

If one byte is aligned, 4 (int) + 1 (char with a length of 1 byte) + 4 (pointer to the virtual function table) = 9




13.

In Java, which of the following statements about method overloading and method rewriting are true?

A. Method Overloading is the same as method rewriting.

B. Method overloading occurs in the parent-child relationship, and method rewriting is in the same category.

C. The return values of method overloading must be of the same type and the parameters must be different.

D. The return values of method rewriting must be of the same type. (Or its subclass)

 



Answer: D

The key to this question is to clarify the concepts of method overloading and method rewriting.

Note that the following conditions are required for methods that can be reloaded:

1. Same method name

2. The number of method parameters is different | the number of method parameters is the same, but at least one pair of corresponding parameters have different types.

3. It is irrelevant to the returned value.

 

Override methods are inherited. To maintain consistency, the return value types must be the same.

 



14.

In the following given program, the function fun function is used to calculate the row subscript of the shortest string in the string array referred to by the ss, and return the row subscript as the function value, and put the string length in the variable referred to by the form parameter n. The number of strings in the string array referred to by ss has a total of M strings, and the length of the string is less than N.
Please fill in the correct content in the program's offline line and delete the offline line so that the program can get the correct result.

Test procedure.


#define M 5#define N 20int fun(char(* ss)[N], int *n){int i, k = 0, len = N;for (i = 0; i < ______; i++){len = strlen(ss[i]);if (i == 0)*n = len;if (len ____ * n);{*n = len;k = i;}}return ( _____ );}main( ){char ss[M][N] = {"shanghai", "guangzhou", "beijing", "tianjing", "chongqing"};int n, k, i;printf("\nThe originalb stringsare:\n");for (i = 0; i < M; i++)puts(ss[i]);k = fun(ss, &n);printf("\nThe length of shortest string is: % d\n", n);printf("\nThe shortest string is: % s\n", ss[k]);}


A. N <k

B. N> k

C. M <k

D. M> k


 

Answer: C

I feel like a question .... The code will be OK after analysis.




15.

Write the output result of the following program

Class A {public: void FuncA () {printf ("FuncA called \ n");} virtual void FuncB () {printf ("FuncB called \ n ");}}; class B: public A {public: void FuncA () {A: FuncA (); printf ("FuncAB called \ n");} virtual void FuncB () {printf ("FuncBB called \ n") ;}}; void main (void) {B B; A * pa; pa = & B; // pa is a pointer to object A. Here, it is bound to object B A * pa2 = new; // pa2 is a pointer to object A, pa-> FuncA (); // FuncA is a common function. Call the FuncA function pa-> FuncB () of Class A here (); // FuncB is a virtual function. Based on the inheritance principle, some sub-classes call subclasses. If the sub-classes do not exist, the parent class is called. Here, the FuncB function pa2-> FuncA () of Class B is called (); // call the FuncA function pa2-> FuncB () of Class A here; // because pa2 is A pointer to Class A, find the FuncB function in Class A. Here, call the FuncB function of Class A delete pa2 ;}


A. FuncA called FuncB called FuncA calledFuncB called

B. FuncA called FuncBB called FuncA calledFuncB called

C. FuncA called FuncBB called FuncAB calledFuncBB called

D. FuncAB called FuncBB called FuncA calledFuncB called

 



Answer: B

For more information, see annotations.

 



16.

In the main () function, after ModifyString (text) is called, what's the value of 'text '?

Int FindSubString (char * pch) // This function is used to search for the retrieval string, and the characters on the left and right sides of the retrieval string are larger than the start and end of the retrieval string, // For example, BCDCB does not work, but ABA can {int count = 0; char * p1 = pch; // note that the pointer pch does not change to while (* p1! = '\ 0') {if (* p1 = p1 [1]-1) {p1 ++; count ++ ;}else {break ;}} int count2 = count; while (* p1! = '\ 0') {if (* p1 = p1 [1] + 1) {p1 ++; count2 -- ;} else {break ;}} if (count2 = 0) return (count); return (0);} void ModifyString (char * pText) {char * p1 = pText; char * p2 = p1; while (* p1! = '\ 0') {int count = FindSubString (p1); // count indicates the number of characters to be skipped. if (count> 0) {* p2 ++ = * p1; sprintf (p2, "% I", count); // overwrite the number of skipped characters in * p1 + 1 while (* p2! = '\ 0') // This while loop is very important. It should be that only when * p2 points to' \ 0, the count cached in the buffer will truly overwrite the value of the text string {p2 ++;} p1 + = count + 1; // p1 skip the retrieval string} else {* p2 ++ = * p1 ++ ;}} void main (void) {char text [32] = "XYBCDCBABABA "; modifyString (text); printf (text );}


A. XYBCDCBABABA

B. XYBCBCDAIBAA

C. xybcscbaibaa

D. XYBCDDBAIBAB

 



Answer: C

Let's take a look at the annotations. I don't quite understand the annotations in some places.

 



17.

The function of the following program is to arrange all the output arrays. Fill in the blanks.

void perm(int list[], int k, int m){if (    ){copy(list,list+m,ostream_iterator<int>(cout," "));cout<<endl;return;}for (int i=k; i<=m; i++){swap(&list[k],&list[i]);(    );swap(&list[k],&list[i]);}}


A. k! = M and perm (list, k + 1, m)

B. k = m and perm (list, k + 1, m)

C. k! = M and perm (list, k, m)

D. k = m and perm (list, k, m)

 

Answer: B

Okay (begin _ success), have you ever seen it in the ACM era? Full string arrangement, using DFS

See NYOJ 32 combination



18.

Write the running results of the following programs.

#include "stdio.h"int sum(int a){auto int c = 0;static int b = 3;c += 1;b += 2;return (a + b + c);}int main(){int i;int a = 2;for (i = 0; i < 5; i++){printf("%d,", sum(a));}}


A. 6, 8, 10, 12, 14,

B. 8, 10, 12, 14, 16,

C., 18

D. 12, 14, 16, 18, 20

 



Answer: B

Another question about water... Static variables are initialized once and exist throughout the life of the function.



19.

#include<iostream>using namespace std;class MyClass{public:MyClass(int i = 0){cout << i;}MyClass(const MyClass &x){cout << 2;}MyClass &operator=(const MyClass &x){cout << 3;return *this;}~MyClass(){cout << 4;}};int main(){MyClass obj1(1), obj2(2);MyClass obj3 = obj1;return 0;}


The output result is ()

A.m. 11214444

B. 11314444

C. 122444

D. 123444.

 

Answer: C

This article mainly discusses C and D selection, which involves shortest replication and deep replication.


MyClass obj3= obj1;  

The copy construction function is called.

If the above line of code is in the following form


MyClass obj3;  obj3= obj1;  

Then it performs deep replication and calls the assignment operators function.

 

20.

What is the output result of the following code?

# Include <stdio. h> char * myString () {char buffer [6] = {0}; // It is stored on the stack and char * s = "Hello World! will be released at the end of the function! "; For (int I = 0; I <sizeof (buffer)-1; I ++) {buffer [I] = * (s + I);} return buffer; // buffer memory will be released, so * buffer becomes a wild pointer} int main (int argc, char ** argv) {printf ("% s \ n ", myString (); return 0 ;}


A. Hello

B. Hello World!

C. Well

D. All of the above are incorrect.

 



Answer: D

For more information, see annotations.

 

It's over .... Continue to tune the camera ....

 




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.