Docsity

Lecture Questions Solutions - Programming Concepts - Java | CSCI 261, Exams of Computer Science

Toggle sidebar
bg1
CSCI261 Lecture Questions Simple C++
This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a
useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in
general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be
happy to review your answers with you during office-hours, via Email, or instant messaging.
1. All of our C++ programs will have a function named: A.
begin
B.
EntryPoint
C.
main
D.
WinMain
Solution: C
2. All C++ statements end with a(n): A. period B. semi-colon C. colon D. flourish
Solution: B
3. (a) Write “
This is a comment
” as a comment in two different ways for C++.
Solution: /*This is a comment */and // This is a comment.
(b) Write the aforementioned commen t across multiple lines in C++.
Solution:
/∗Thi s
i s a
comm en t ∗/
/ / T hi s
/ / i s a
/ / c omme nt
4. True or False: Indentation is required for the compiler to understand the logical structure of a C++ source listing.
Solution: False. Indentation is to help humans visualize the structure of program logic.
5. True or False: Everything in a C++ source listing is translated to the computer program.
Solution: False. Comments and indentation are ignored.
6. True or False: A computer program is the compiler and linker translation of only the program source listing.
Solution: False. Pre-processor directives “import” code from system libraries.
7. Which of the following properties does a program variable possess?
A. Size (in bits or bytes)
B. Location (address in memory)
C. URL (universal resource locator)
D. Name
E. Value
pf3

Partial preview of the text

Download Lecture Questions Solutions - Programming Concepts - Java | CSCI 261 and more Exams Computer Science in PDF only on Docsity!

This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be happy to review your answers with you during office-hours, via Email, or instant messaging.

  1. All of our C++ programs will have a function named: A. begin B. EntryPoint C. main D. WinMain

Solution: C

  1. All C++ statements end with a(n): A. period B. semi-colon C. colon D. flourish

Solution: B

  1. (a) Write “This is a comment” as a comment in two different ways for C++.

Solution: /* This is a comment */ and // This is a comment.

(b) Write the aforementioned comment across multiple lines in C++.

Solution:

/ ∗ T h i s i s a comment ∗ /

/ / T h i s / / i s a / / comment

  1. True or False: Indentation is required for the compiler to understand the logical structure of a C++ source listing.

Solution: False. Indentation is to help humans visualize the structure of program logic.

  1. True or False: Everything in a C++ source listing is translated to the computer program.

Solution: False. Comments and indentation are ignored.

  1. True or False: A computer program is the compiler and linker translation of only the program source listing.

Solution: False. Pre-processor directives “import” code from system libraries.

  1. Which of the following properties does a program variable possess? A. Size (in bits or bytes) B. Location (address in memory) C. URL (universal resource locator) D. Name E. Value

Solution: A, B, D, and E.

  1. What is another name for the name given to a variable?

Solution: An identifier.

  1. List three examples of valid C++ variable names.

Solution: x3, SuperMan, mighty_mouse

  1. List two examples of invalid C++ variables names, and state why they are invalid.

Solution: 3x: numerals cannot begin variable names. CO2%: The percent sign is an invalid character for variable names.

  1. In the following table are four fundamental variables types in C++, list example values for each varible type.

double 3.14, -1, 0 , 32 int -3, 0 , 256 string "c", "word", or "a phrase of several words" bool true, false

  1. Which of the fundamental variable types in 11 require(s) a pre-processor directive and a compiler directive? What are they?

Solution: The string type requires #include , and using namespace std;.

  1. Write two ways to declare a Boolean variable named truth as logical false.

Solution: bool truth( false ); or bool truth = false;.

  1. Write two ways to declare an integer variable named N with the value 3.

Solution: int N( 3 ); or int N = 3;.

  1. Write two ways to declare a constant real valued variable named PI as the value π.

Solution: const double PI( acos(-1) ); or double PI = acos(-1);.

  1. Assume the real valued variables x, y, and z are properly declared in a C++ program, complete the table by translating the mathematical expression to a C++ statement.

Mathematical Expression C++ Statement x^2 + y − (^1) z xx + y - 1/z √ 3 x − 4 y + (^) zyx sqrt( 3x - 4y) + x/(zy)

Page 2