[C++ written test strong training] the seventh day

multiple choice

In the case of (), it is appropriate to use inline to define the inline function
A. The function body contains loop statements . B. The function
body contains recursive statements
.

Review the inline function: the inline modified function is called an inline function. In the compilation stage, the inline function will be expanded - the function call is directly replaced by the function body, which reduces
the cost of pushing the parameters of the function call to the stack and creating a stack frame , which can improve the efficiency of program operation.
Note: inline is a suggested keyword - the specific situation depends on the compiler, which is determined by the compiler.
Generally, it is recommended: no loop, no recursion, and the function body is not long. So it is obvious to choose C

In the C++ language, the correct description of the default value of the function parameter is ()
A The parameter with the default value of the function can only have one
B If there are multiple parameters of a function, the setting of the default value of the parameter can be discontinuous
C Function parameter The default value must be set
D After setting the default value of the parameter, all the parameters defined after this parameter must be set to the default value

Default parameter (default parameter): When declaring and defining a function, you can bring a default value to the parameter of the function; if the user does not pass an actual parameter, the given default value will be used.
It can be fully defaulted, each parameter has a default value, or semi-defaulted, some parameters have default values, and the semi-defaulted default values ​​must be given from right to left, for example:
void f(int a, int b = 20, int c = 10)
so the answer is D

Which of the following statements about class definitions is correct:
A class definition includes the declaration of data members and function members
The default access rights of members of class B are protected
C data members must be declared as private
D member functions can only be used in define outside the class

There are 2 ways of class definition:

1. Put all declarations and member function definitions in the class
2. Put member variables and member function declarations in the class, and member function definitions can be placed in .cpp. Note that the class name::
class member must be added before the member function name The default access right of struct is private (private) , and the default access right of struct is public. It is wrong that data members must be declared as private; member functions can also be defined in the class, so choose A

Assume that the constructor of a class is A(int aa,int bb){a=aa–;b=a*bb;}, then execute A x(4,5); after the statement, the values ​​of xa and xb are ( )
A 20 and 5
B 3 and 15
C 5 and 4
D 4 and 20

a=aa–; post--, it is assigned first and then –-, so a is still equal to 4; bb is 5, 4*5 = 20, choose D

Which of the following statements about constructors is correct?

A Constructor can declare the return type
B Constructor cannot be modified with private
C Constructor must be the same as the class name
D Constructor cannot take parameters

Review the characteristics of the constructor: the constructor has no return value, must be the same as the class name, and cannot take parameters.
In general, the permissions of the constructor are public, because when creating an object outside the class, the compiler needs to call the constructor (special : Singleton mode - a class can only create one object, set the constructor as private)
compare options, the answer is C

There is a class A whose data members are as follows:

class A {
...
private:
	int a;
public:
	const int b;
	float* &c;
	static const char* d;
	static double* e;
};

Then in the constructor, the member variables must be initialized through the initialization list: ______.
A abc
B bc
C bcde
D bcd
E b
F c

The position of the initialization list is the real initialization, and the constructor body is only assigned. In the
initialization list: only initialize the non-static member variables in the class (static ones are shared by everyone and placed outside the class)
must be initialized in the initialization list Of:
1. const modified member variable
2. member variable of reference type
3. class type object, this class has no default constructor,
so the answer is B

There are the following class template definitions: ()

template<class T> class BigNumber{
	long n;
public:
	BigNumber(T i) :n(i) {}
	BigNumber operator+(BigNumber b) {
	return BigNumber(n + b.n);
	}
};

Given that b1 and b2 are two objects of BigNumber, the error in the following expression is ()
A 3+3
B b1+3
C b1+b2
D 3+b1

The operator is overloaded as a member function of the class, and the number of formal parameters is one less than that required by the operator, because the member function implies the this pointer.
A: It is possible to directly add built-in types;
B: b1+3; + is the addition of two BigNumber objects.
3 is not an object, if the class has a single-parameter constructor, the constructor has the function of type conversion
b1+3; the compiler will call the single-parameter constructor to convert 3 into a BigNumber object
D: first The first parameter must be a BigNumber object, because it is the this pointer, so choose D

Regarding the difference between friend functions and member functions, which one is wrong?
A friend function allows this class and friend class objects to call
B. Both friend function and class member functions can access the private member variables or member functions
of the class. The member functions of class C belong to the class and are called through The
D friend function called by the pointer this is decorated with the keyword friend, and it is also called through the pointer this when calling

Friend function: not a member function of a class, without this pointer, but private members of the class can be accessed inside the friend function
Member function: has a hidden this, is constrained by the access qualifier, and is called through the object,
so the answer is D

For the following code, the correct statement is ()

char * p = new char[100];

The memory from A p and new is on the stack
B The memory from p and new is on the heap
C p is on the stack The new one is on the heap
D p is on the heap The new one is on the stack

new: C++ is used to dynamically apply for space. By default, the bottom layer of new calls operator new ——> malloc, so by default, the requested space is on the heap. The
pointer p is in the data segment in the global scope. The function body is on the stack,
so the answer is C

The use of class templates is actually the instantiation of class templates into a concrete __________.
A class
B function
C template class
D object

The use of class templates is actually the instantiation of class templates into a concrete class


programming questions

Legal bracket sequence judgment

image-20221213224024573

Unable to match

1. The current character is not a bracket character . 2. The bracket
matching is incomplete. There
is an extra left half; If there
is no left parenthesis
and the stack is empty, it means that the match is incomplete and there is an extra right parenthesis
. Yes, pop out the stack and match a complete parenthesis. If the stack is not empty after traversing, it means that there are more left parentheses

The idea is relatively simple, the code implementation:

class Parenthesis {
public:
    bool chkParenthesis(string A, int n) {
        // write code here
        stack<char> sc;
        for(auto e: A)
        {
            switch(e)
            {
                case '(':
                    sc.push(e);
                    break;
                case ')':
                    if(sc.empty())
                    {
                        return false;
                    }
                    sc.pop();
                    break;
                default:
                    return false;
            }
        }
        return sc.empty();
    }
};

Fibonacci sequence

image-20221213230327383

Find the two fib numbers closest to N, one less than N and one greater than N.

N-left,right-N

The closest distance is: min(N - left, right - N)

#include <iostream>
using namespace std;
int main() {
    int N,left=0,right=0;
    int f,f1=0,f2=1;
    cin>>N;
    while(1)
    {
        f = f1 + f2;
        f1 = f2;
        f2 = f;
        if(f<N)
        {
            left = f;
        }
        else
        {
            right = f;
            break;
        }
    }
    cout<<min(N-left,right-N)<<endl;
}

Guess you like

Origin blog.csdn.net/weixin_60478154/article/details/128311185