C ++ basic knowledge interview featured 100 question series (11-20 questions) [C ++ basics]

Source: Internet
Author: User

Run the C # code below. What is the output? Namespace StringValueOrReference {class Program {internal static void ValueOrReference (Type type) {String result = "The type" + type. name; if (type. isValueType) Console. writeLine (result + "is a value type. "); else Console. writeLine (result + "is a reference type. ");} internal static void ModifyString (String text) {text =" world ";} static void Main (string [] args) {String text =" hello "; Val UeOrReference (text. GetType (); ModifyString (text); Console. WriteLine (text) ;}} [Analysis] outputs two rows. The first line is The type String is reference type. The second line is hello. The String type is defined as public sealed class String {...}. Since it is a class, String is the reference type. In the ModifyString method, assign a new string to text, instead of the original text content, but point text to a new string "world ". Since the text parameter does not contain ref or out, after the method is output, the text still points to the original string, so the output is still "hello ". [Question 12] What is the output of the running C ++ code? # Include <iostream> class A {private: int n1; int n2; public: A (): n2 (0), n1 (n2 + 2) {} void Print () {std: cout <"n1:" <n1 <", n2:" <n2 <std: endl ;}}; int _ tmain (int argc, _ TCHAR * argv []) {A a;. print (); return 0;} [Analysis] Output n1 is a random number, n2 is 0. In C ++, the initialization sequence of member variables is the same as the declarative sequence of variables in the type, but it is irrelevant to the sequence in the initialization list of the constructor. Therefore, in this question, n1 is initialized first, while n2, the initial n1 parameter, is a random value. Therefore, n1 is a random value. When n2 is initialized, It is initialized according to the parameter 0, so n2 = 0. [Question 13] What is the result of compiling the running C ++ code? (A) Compilation error; (B) successful compilation, program crash during running; (C) normal compilation and 10 output. Select the correct answer and analyze the cause. # Include <iostream> class A {private: int value; public: A (int n) {value = n;} A (A other) {value = other. value;} void Print () {std: cout <value <std: endl ;}; int _ tmain (int argc, _ TCHAR * argv []) {A a = 10; A B = a; B. print (); return 0;} [Analysis] compilation error. The input parameter in the copy constructor is an instance of. The copy constructor is called to copy the form parameter to the real parameter. Therefore, if the copy constructor is allowed to pass values, an endless recursion will be formed and stack overflow will occur. Therefore, the C ++ standard does not allow copying constructors to pass value parameters. Instead, it must be a reference or constant reference. In Visual Studio and GCC, compilation errors occur. [Question 14] What is the output of the running C ++ code? # Include "stdafx. h "int SizeOf (char pString []) {return sizeof (pString);} int _ tmain (int argc, _ TCHAR * argv []) {char * pString1 = "google"; int size1 = sizeof (pString1); int size2 = sizeof (* pString1); char pString2 [1, 100] = "google "; int size3 = sizeof (pString2); int size4 = SizeOf (pString2); printf ("% d, % d", size1, size2, size3, size4); // 1,100, return 0;} [Analysis] 4, 4. PString1 is a pointer. On 32-bit machines, any pointer occupies 4 bytes of space. * PString1 is the first character of the string pstring1. A character occupies one byte. PString2 is an array, and sizeof (pString2) is used to evaluate the size of the array. The array contains 100 characters, so the size is 100 bytes. In the SizeOf function, although the input parameter is a character array, when the array is passed as a function parameter, the array will automatically degrade to the same type of pointer. Therefore, size4 is also the pointer size, which is 4. [Question 15] What is the output result of the running code? What is the problem with this code? How can I correct it? # Include "stdafx. h "# include <iostream> class A {public: A () {std: cout <" A is created. "<std: endl ;}~ A () {std: cout <"A is deleted. "<std: endl ;}; class B: public A {public: B () {std: cout <" B is created. "<std: endl ;}~ B () {std: cout <"B is deleted. "<std: endl ;}; int _ tmain (int argc, _ TCHAR * argv []) {A * pA = new B (); delete pA; return 0;}/* A is created. B is created. A is deleted. */[Analysis] Output three rows: A is created. B is created. A is deleted. This will cause memory leakage and add the virtual keyword before the destructor of. When B is created with new, the constructor B is called back. When calling the constructor of B, the constructor of A is called first. Therefore, output A is created. B is created. When the delete statement is run, the Destructor is called. Since pA is declared as A pointer of type A, and the destructor of base class A is not labeled as virtual, only the destructor of Class A is called, instead of calling the destructor of B. Because pA is actually A pointer to a B instance, but only calls the destructor of the base class A in the analysis, but does not call the destructor of B, this causes memory leakage. If some resources, such as file handle and memory, are created in type B, they are not released, resulting in resource leakage. [Question 16] run the following C ++ code. What is output? # Include "stdafx. h "# include <iostream> class A {public: virtual void Fun (int number = 10) {std: cout <" :: fun with number "<number ;}}; class B: public A {public: virtual void Fun (int number = 20) {std: cout <" B :: fun with number "<number ;}}; int main () {B B; A & a = B;. fun ();} [Analysis] Output B: Fun with number 10. Because a is a reference to B instance, B: Fun is called during running. However, the default parameters are determined during the compilation period. During compilation, the compiler only knows that a is a reference of type A. The specific type pointed to cannot be determined during the compilation period. Therefore, :: fun's statement sets the default parameter number to 10. The key to this question is to understand how to determine the default parameter value during compilation, but to determine which type of function is called by the referenced or pointer virtual function. [Question 17] run the following C code. What is the output? # Include "stdafx. h "char * GetString1 () {char p [] =" Hello World "; return p;} char * GetString2 () {char * p =" Hello World "; return p;} int _ tmain (int argc, _ TCHAR * argv []) {printf ("GetString1 returns: % s. \ n ", GetString1 (); printf (" GetString2 returns: % s. \ n ", GetString2 (); return 0;} [Analysis] outputs two rows. The first row GetString1 returns is followed by a random string of content, the second line is GetString2 returns: Hello World. the difference between the two functions is that GetString1 is an array, while GetSt Ring2 is a pointer. When running to GetString1, p is an array that opens up a piece of memory and copies "Hello World" to initialize the array. Return the first address of the array and exit the function. Since p is a local variable in GetString1, the memory of this array will be released when it runs outside the function. Therefore, when accessing the content of this array in the _ tmain function, the result is random. When running to GetString2, p is a pointer pointing to a constant string in the String constant area. The constant string is global and will not be released because it exits the GetString2 function. Therefore, in _ tmain, the string "Hello World" is still obtained based on the address returned by GetString2 ". [Question 18] What is the output result of running C code? # Include "stdafx. h "int _ tmain (int argc, _ TCHAR * argv []) {char str1 [] =" hello world "; char str2 [] =" hello world "; char * str3 = "hello world"; char * str4 = "hello world"; if (str1 = str2) printf ("str1 and str2 are same. \ n "); else printf (" str1 and str2 are not same. \ n "); if (str3 = str4) printf (" str3 and str4 are same. \ n "); else printf (" str3 and str4 are not same. \ n "); return 0;} [Analysis] outputs two rows. The first line is str1 and str2 are not same, and the second line is str3 and str4 are same. Str1 and str2 are two string arrays. We will allocate two 12-byte spaces for them, and copy the "hello world" contents to the array respectively. These are two arrays with different initial addresses. Therefore, the values of str1 and str2 are different. Str3 and str4 are two pointers. Instead of allocating memory to them to store string content, we only need to point them to the address in the memory of "hello world. Because "hello world" is a constant string and has only one copy in the memory, str3 and str4 point to the same address. Therefore, comparing str3 and str4 values is the same. [Question 19] What is the result of running C ++ code? # Include "stdafx. h "bool Fun1 (char * str) {printf (" % s \ n ", str); return false;} bool Fun2 (char * str) {printf ("% s \ n", str); return true;} int _ tmain (int argc, _ TCHAR * argv []) {bool res1, res2; res1 = (Fun1 ("a") & Fun2 ("B") | (Fun1 ("c") | Fun2 ("d ")); res2 = (Fun1 ("a") & Fun2 ("B") & (Fun1 ("c") | Fun2 ("d ")); return res1 | res2;} [Analysis] Print four rows, namely a, c, d, and. In C/C ++, and or operations are executed from left to right. When calculating rest1, calculate Fun1 ("a") & Func2 ("B") first "). First, Func1 ("a") prints a row whose content is. Since Fun1 ("a") returns false, whether the return value of Func2 ("B") is true or false, Fun1 ("a") & Func2 ("B ") the result is false. Because the results of Func2 ("B") are irrelevant, Func2 ("B") will be omitted without computation. Next, calculate Fun1 ("c") | Func2 ("d") and print the two rows of content c and d respectively. When calculating rest2, Func1 ("a") First prints a row whose content is. Since Func1 ("a") returns false, Func2 ("B") will skip the calculation in the same way as above. Since the result of Fun1 ("a") & Func2 ("B") is false, no matter what the result of Fun1 ("c") & Func2 ("d") is, the result of the entire expression is false, so Fun1 ("c") & Func2 ("d") will be ignored. [Question 20] run the following C ++ code. What is the printed result? # Include "stdafx. h "# include <iostream> using namespace std; class Base {public: void print () {doPrint ();} private: virtual void doPrint () {cout <" Base:: doPrint "<endl ;}; class Derived: public Base {private: virtual void doPrint () {cout <" Derived: doPrint "<endl ;}}; int _ tmain (int argc, _ TCHAR * argv []) {Base B; B. print (); Derived d; d. print (); return 0 ;}

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.