Chapter 1
Introduction to C++ Q1)
Choose the correct answer of the following multiple choice questions:1. Which of the following statements about C++ is false?
(a) C++ is an extension of C
(b) C++ provides capabilities of object-oriented programming
(c) C++ is an interpreted language
(d) C++ is a compiled language
************************************************************************2. Which of the following languages was not an ancestor of C++?
(a) C (b) B (c) A (d) BCPL
3. Computer programs are
(a) sets of control instructions
(b) the information processed by the computer
(c) the various devices (disks, memory, keyboard) that comprise a computer system
(d) the people programming the computers
*********************************************************************4. Pseudocode does not include
(a) declarations
(b) input/output
(c) algorithms
(d) control structures
*********************************************************5. The compile stage is when ____________.
(a) the object code is linked with code for functions in other files
(b) the C++ program is translated into machine language code
(c) the program is executed one instruction at a time
(d) the program is placed in memory
******************************************************************6. Specifying the order in which statements are to be executed in a computer program is called
(a) an algorithm (b) transfer of control (c) program control (d) pseudocode
*******************************************************************7. Lines beginning with _______________ inform the compiler that the rest of the line is a comment.
(a) // (b( ** (c) REM (d) <<
************************************************************** ***8. Which of the following must every executable C++ program have?
(a) main (b) /**/ (c) // (d) std::cout
********************************************************* **9. Every statement in C++ must end with a
(a) period (.) (b) semicolon (
(c) colon (
(d) backslash (/)
******************************************************** **10. Which of the following is not a valid escape sequence?
(a) \n (b) \\ (c) \~ (d) \"
********************************************* *11. Which of the following is not a valid integer value?
(a) -3 (b) 0 (c) 2134859 (d) 1.1
****************************************************************12. Which of the following is an invalid identifier (variable name)?
(a) _Test (b) TEST (c) 5test (d) test1
*****************************************************************************13. Which statement prints "hi" on the screen?
(a) cout << "hi"; (b) cout >> "hi"; (c) "hi" >> cin; (d) cin >> "hi";
*****************************************************************************14. Which operation returns the integer remainder when 15 is divided by 6?
(a) 15 / 6 (b) 15 % 6 (c) 15 ^ 6 (d) 15 * 6
* *********************************************************15. Evaluate the expression
3 * 4 % 6 + 4 * 5
(a) 20 (b) 26 (c) 12 (d) 32
****** **************************************************16. Which of the following is an equality operator?
(a) == (b) = (c) > (d) >=
********************** **********************************17. ______ is a set of instructions written in a computer programming language that processes data to solve a specific task.
(a) data (b) pseudocode (c) program (d) computer programming
*************************************************** ***********18. In C++ environment, the phase that ties/connects the compiled object code of the program with the necessary standard and private libraries to the program is called:
(a) editing phase
(b) preprocessing phase
(c) compilation phase
(d) linking phase
(e) none of the above
**************************************************** *******19. C++ is:
(a) high level, reusable, but not event-driven language
(b) portable, low level, but not reusable language
(c) structured-oriented, object-oriented, but not portable language
(d) object-oriented, low level, but not structured-oriented language
**** *******************************************************20. One of the following are all valid variable names:
(a) student_first_name, c3, m1234, _grade_
(b) student_first_name, c3, m12-m34, grade_
(c) student_firstname, 3c3, m1234, grade_
(d) student first name, c3, m1m2m3m4m, grade_
************************************************* ********21. Variable names actually correspond to __________.
(a) locations in the computer's memory (b) operators
(c) integers (d) data types
******************************************* *****************22. The value of s after performing the statement: s = 3 + 5 – 3 % 2 == 5 / 5 * 2 || !3 < 4 * 2 ;
(a) false (b) true (c) 21 (d) 24
****************************************************************************23. Given the algebraic equation y = ax3 + 7, which of the followings are correct C++ statements for this equation:
(a) y = a * x * x * ( x + 7 ) ;
(b) y = ( a * x ) * x * ( x + 7 ) ;
(c) y = a * x * x * x + 7 ;
(d) y = a * x * ( x * x + 7 ) ;
(e) (a) and (b)
******************************************************* ***24. Assume that
int x = 0, y = 10;
then the statement:
cout << x/y << endl ;
(a) has no error
(b) has a logical error
(c) has a syntax error (compile time error)
(d) has a run time error
***** ****************************************************25. Assume that
int x = 0, y = 10;
then the statement:
cout << y/x << endl ;
(a) has no error
(b) has a logical error
(c) has a syntax error (compile time error)
(d) has a run time error
****************************************************** *******26. What will the following statement print
cout << "\"*\"\n\?**\?\n\"***\"\n" ;
(a) "*"
?**?
"***"
(b) "*"
"?**?"
"***"
(c) "\"*\"\n\?**\?\n\"***\"\n"
(d) \"*\" \?**\? \"***\"
*********************************************** **************27. What is the punctuation used to signal the beginning and end of code blocks?
(a) { } (b) -> and <- (c) ( and ) (d) BEGIN and END
**************************************** *************28. What is the output of the following statement
cout << fixed << left
<< setprecision ( 1 ) << setw ( 11 ) << 333.546372
<< setprecision ( 2 ) << setw ( 12 ) << 333.546372
<< setprecision ( 3 ) << setw ( 13 ) << 333.546372 ;
(a) 3.546372 33.546372 333.546372
(b) 333.5 333.55 333.546
(c) 333.546372 333.546372 333.546372
(d) 333.5 333.54 333.546
****************** ************************ *******************29. One of the following declarations and initializations is not correct
(a) const int x; (b) const int x=6; (c) int x; (d) int x=6;
**************************************************** ***********30. Given the following declarations
int x = 5,y=4;
what will the following statement print
cout<<(x == ++y ? "Ok": "Not Ok");
(a) Ok (b) Not Ok (c) Ok Not Ok (d) will produce an error
****************************************************31. Which of the following is not a binary operator
(a) * (b) && (c) ++ (d) /=
32. Which of the following will not increment variable c by one?
(a) c + 1; (b) c++; (c) ++c; (d) c += 1;
33. Which of the following is a correct comment?
(a) */ Comments */ (b) ** Comment ** (c) // Comment (d) { Comment }
34. Which of the following is not a correct data type?
(a) float (b) real (c) int (d) double
35. In C++, the condition 4 > y > 1
(a) evaluates correctly and could be replaced by ( 4 > y && y > 1 )
(b) does not evaluate correctly and should be replaced by ( 4 > y && y > 1 )
(c) evaluates correctly and could not be replaced by ( 4 > y && y > 1 )
(d) does not evaluate correctly and should not be replaced by ( 4 > y && y > 1 )
36. What is the final value of x after performing the following operations?
int x = 21 ;
double y = 6 ;
double z = 14 ;
y = x / z ;
x = 5.5 * y ;
(a) 8.25 (b) 5.5 (c) 5 (d) 8
37. If x = 3, which of the following sets x to 7?
(a) x *= 4; (b) x += 4; (c) x =+ 4; (d) x + 4 = x;
38. In which of the following is y not equal to 5 after execution? Assume x is equal to 4.
(a) y = 5; (b) y = x++; (c) y = ++x; (d) y = x = 5;
39. Which of the following data types can be used to represent integers?
(a) char
(b) long
(c) short
(d) all of the above
40. Which of the following stream manipulators used to set the field width (i.e. no of spaces)
(a) left (b) setwidth (c) setprecision (d) setw
41. Variables are also known as
(a) lvalues, but can be used as rvalues
(b) lvalues, and cannot be used as rvalues
(c) rvalues, and cannot be used as lvalues
(d) constant variables
42. The statement z *= z + y – x ; is equivalent to
(a) z = z * z + y – x ;
(b) z = z * ( z + y – x ) ;
(c) z = z + y – x ;
(d) none of the above
43. The value of the following statement is
( 6 * 3 == 36 / 2 ) || ( 13 < 3 * 3 + 4 ) && !( 6 – 2 < 5 )
(a) 0 (b) 1 (c) 9 (d) 18
44. Which of the following data types can be used to represent integers?
(a) char
(b) long
(c) unsigned int
(d) (b) and (c)
(e) all of the above
45. The value of x after performing the statement: x = ( 3 + 9 * (3 - ( 3 % 9 / ( 3 ) ) ) ) ; is
(a) 21 (b) 24 (c) 36 (d) 39
46. If x=2, y=3, z=4 then, the value of z after performing the expression z *= z + y - x is
(a) 5 (b) 9 (c) 17 (d) 20
47. The following declarative statement
#define STOP 999
(a) means that STOP is a variable carrying the value 999
(b) means that STOP is a constant carrying the value 999
(c) is used to print “999”
(d) is used to print “STOP 999”
48. Which of the following escape sequences is used to represent a new line?
(a) \a (b) \t (c) \n (d) \b
49. Which of the following escape sequences is used to make a beep (alert)?
(a) \a (b) \t (c) \n (d) \b
50. If x=4 then, the value of the expression x++ * x % ++x is
(a) 0 (b) 1 (c) 2 (d) 3
51. If x=3, y=5, z=2 then, the value of z after performing the expression z *= y - x + y++ / z - y is
(a) 1 (b) -0.5 (c) -1 (d) -1.5
52. #include < … > indirective statement is executed by
(a) preprocessing phase (b) compiling phase (c) linking phase (d) loading phase
53. the break statement causes
(a) exit from entire program (b) exit from the current loop
(c) exit from the current function (d) exit from the current header
54. The linker creates __________
(a) an executable image (b) a preprocessor directive
(c) object code (d) an input stream
55. The statement using allows us to type __________ instead of ___________.
(a) print, std::cout (b) std, std::cout
(c) cout, std::cout (d) cout, std
56. When a number gets assigned to a variable that already has a value
(a) the new number overwrites the previous value at that memory location
(b) the new number gets assigned to a neighboring memory location
(c) the computer issues an error
(d) the new value is destroyed and the old value remains
57. An example of a unary operator is
(a) a relational operator
(b) an assignment operator
(c) an increment operator
(d) a logical operator
58. What is the output when the { int i= 5; cout << ( --i += 3 ); } is run?
(a) nothing
(b) 7
(c) 8
(d) it will not run properly
Q2) Answer the following questions:
(a) Define Data.
(b) Define Computer Program.
(c) What do we mean by Programming Methodology? Name four Programming Methodologies.
(d) What is the difference between Structured Programming and Object-Oriented Programming?
Name a programming language for each type of these two methodologies.
(e) Define Programming Language.
(f) Define Software. Can we equate the term Software with Computer Program? Why?
(g) What is the difference between Program Design and Program Implementation?
(h) Define the Algorithm. State four different ways to express an algorithm.
(i) What is a Pseudocode? Write a simple example.
(j) What is the stream file? Give two examples of stream files.
(k) What is the difference between Header file and CPP file?
(l) Justify (give reason for) not placing semicolon (
at the end of declarative statements.
(m) State the three parts that all C++ system consists of?
(n) In C++ Environment, State the six phases that all C++ programs should gone through them in order to be executed.
(o) State the Types of Errors in programs. Write an example for each type of error.
(p) What is the difference between Identifier and Variable? Support your answer with examples.
(q) Mention three main Features of C++ programming language.
(r) Explain the critical difference between 3, ’3’ and "3"
(s) What does the command #include accomplish?
(t) What are the general rules for naming variable?
(u) How can we get the ASCII value of the character constant?
(v) What’s the difference between a signed integer and an unsigned integer?
(w) Give an example of an unsigned data value.
(x) Would the following assignment store a 1 (true) or a 0 (false) in Answer?
Answer = ( 4 == 4 == 4 );
Trace the following and write the output:
(i)
If x=2, y=3, z=4
Find out the value of the followings separately:
(a) z *= z + y – x z =…
(b) y = ++x x =… y =…
(c) y = x++ x =… y =…
Output:
(a) z=20
(b) x=3 y=3
(c) x=4 y=3
(ii)
Assume the following declarations and initializations:
int a, c, f=11, e=3;
double b=10, x=9, y=13;
then, calculate the following expressions according to the operators priorities:
a = 7 + 5 * 2 + ( 3 + 4 * 2 + 1 );
b += 7 / 2 * 7 / 2.0 + x / 2 – y / 2.0;
c = ++f + e-- + 33 % 10 – 7 % 8;
cout << "a=" << a << "\t"<<"b=" << b << "\t"<<"c=" << c << endl;
Output:
a=29 b=18.5 c=11
(iii)
If x=3, y=4, z=5
Calculate the following expressions separately and step by step according to the operators priorities (precedence):
(a) x *= x--
(b) x += z *= x + y
(c) x + z – 3 % 2 == z / z * 2 || !x – 3 < y * 2
Output:
(a) x=8
(b) x=38
(c) True or 1