C++ Final Exam

download C++ Final Exam

of 51

description

exam

Transcript of C++ Final Exam

  • Consider the following statements. string str1 = "ABCDEFGHIJKLM"; string str2; After the statement str2 = str1.substr(1,4); executes, the value of str2 is "___". Given the function prototype: float test(int, int, int); which of the following statements is legal? In C++, the scope resolution operator is ____. Suppose that sum and num are int variables and sum = 5 and num = 10. After the statement sum += num++; executes ____. A variable listed in a function call is known as a(n) ____ parameter. A variable list in a header is known as a(n) ____ parameter. int alpha = 5; int beta = 10; alpha = alpha + 5; { int alpha = 20; beta = beta + 5 ; } cout
  • value is 5? cin >> num; if (num > 0) num = num + 10; else if (num == 5) num = num + 15; Suppose j, sum, and num are int variables, and the input is 26 34 61 4 -1. What is the output of the code? sum = 0; cin >> num; for (int j = 1; j > num; } cout
  • Suppose that x is an int variable. Which of the following expressions always evaluates to true? Given int one; double two; bool four; which of the following assignments are valid? (i) one = 7 * 3 % 4; (ii) 2.3 + 3.5 = two; (iii) four = (2 5) y = 1; else if (x < 5) { if (x < 3) y = 2; else y = 3; } else y = 4; What is the value of y if x = 6? Which of the following function prototypes is not valid? int funexp(intx,intv); int funexp(intx,intv){};???? ?? funexp(void); int funexp(void); Which of the following is NOT a reserved word in C++? int cons?

  • num???? ?? Assume all variables are properly declared. What is the output of the following C++ code? num = 100; while (num
  • Which of the following correctly declares and initializes alpha to be an array of 4 rows and 3 columns and the component type is int? Given the function prototype: float test(int, int, int); which of the following statements is legal? Suppose that x = 25.67, y = 356.876, and z = 7623.9674. What is the output of the following statements? cout
  • alpha = alpha + 3; case 8: alpha = alpha + 4; break; default: alpha = alpha + 5; } Suppose that you have the following function. void mystery(int& one, int two) { int temp temp = one; one = two; two = temp; } What are the values of x and y after the following statements? (Assume that variables are properly declared.) x = 10; y = 15; mystery(x, y); Consider the following statement. int y = !(12 < 5 || 3 x) ? 7 : 9; What is the value of y if x = 2? Suppose that x = 87 and y = 423. What is the output of the following statements? cout
  • Consider the following declaration. char charArray[51]; char discard; Assume that the input is: Hello There! How are you? What is the value of discard after the following statements execute? cin.get(charArray, 51); cin.get(discard); ???? ???? Suppose that x and y are int variables. Which of the following is a valid input statement? The output of the statement: cout y; Assume that alpha is a character array of size 20. Which of the following statements is illegal in C++?

  • - 1 -

    Answer all questions

    Q1. Write a one line C++ statement to do the following:

    a. An infinite while loop, without any statement inside the while loop

    block, using the expression (x < 10) as the decision maker, not x is

    declared as in integer?.

    b. Declare a character array to a store a name of a student of 30 character

    maximum for 100 students?

    c. A call to function fctx which return no value- the call passes the array

    student with is declared as int students (5) (10)? Assume all needed

    variables are declared".

    d. In the previous question, what would be the function

    declaration/header.

    e. Declares the integer array test- score (100) and initializes only the first

    element of the array to 10?

    Q.2. given the following structure that contains the social security number

    and the year ob birth:

    Struct into (unsighed long soc sec/num).

    Unsigned int year birth).

    a. Write a structure called Boo di that contains the following members:

    info structure, name of type, string.

    b. write a function named initialize info that takes no parameters but

    return a structure of type info in which the social security number is

    initialized to 5511, the name is initialized to Ahmed, and the year to

    1986.

    c. Write a main function (including all necessary header files and other

    parameters) to declare an identifier of type Boo di, initialize, Boo Di by

    calling the function initialize info.

  • - 2 -

    Multiple choice:

    Identify the letter of the choice that best complete the statement or

    answers the question.

    1. Which of the following is not a valid C++ identifier?

    a. stop and go.

    b. salary for the month.

    c. hello three.

    d. My counter.

    2. which of the following is a reserved word in C++?

    a. char.

    b. char.

    c. CHAR.

    d. None of these.

    3. The value of the C++ expression 14/4 + 4.3 is:

    a. 7

    b. 7.3

    c. 7.8

    d. none of these.

    4. Suppose that alpha is a double variable. What is the value of alpha after

    the following statement executes:

    a. alpha 14.0 + static- cast < double> (15/2):

    a. alpha = 21.0.

    b. alpha = 21.5.

    c. alpha = 22.0.

    d. none of these.

  • - 3 -

    5. Suppose that X and y are int variables, ch is a char variable, and the

    input is 4 2 A 12.

    What is the value of x, y, and Ch after the following statement executes

    > x >> ch>>y,

    a. x = 4, ch = 2, y= 12.

    b. x 4, ch= A, y = 12.

    c. x = 4, ch = ', y = 2.

    d. this statement results in input failure.

    6. Suppose that X = 55.68, y = 476,859 and z = 23, 8216, what is the

    output of the following statement?

    Cout

  • - 4 -

    Hi 32 62.93 781.92.

    c. 12345678901234567890j.

    Hi 32 62.93 781. 92.

    d. None of these.

    8. after the execution of the following code, what will be the value of

    num ifthe input values are 4 5 ?

    Cin >> num.

    If (num > 0).

    Num = num > 5).

    Num = num + 15'.

    a. 4.

    b. 5.

    c. 14.

    d. 15.

    9. What is the output of the following C++ code ?

    Int x = 35,

    Int y = 45.

    Int z,

    If (x > y).

    Z = x + y1

    Else

    Z = y x

    Cout

  • - 5 -

    10. What is the output of the following code fragment if the input value is

    4?

    Int num'

    Int alpha = 10.

    Cin >> num,

    Switch (num).

    Case 3: alpha ++.

    Break;

    Case 4:

    Case 6: alpha = alpha + 3.

    Case 8: alpha + 4.

    Break,

    Default: alpha = alpha + 5'.

    Cout

  • Noname ++ CC++ Final Exam