C++ Chapters 1-4

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Which symbol is commonly used for incrementing a variable by 1 in the "update" part of a for loop?

++

What is the C++ syntax for a single-line comment?

// This is a comment

What is the value of x after the following code executes? int x; x = 3 / static_cast<int>(4.5 + 6.4);

0

What is the output of the following code? int w = 98; int x = 99; int y = 0; int z = 1; if (x >= 99) { if (x < 99) cout << y << endl; else cout << z << endl; } else { if (x == 99) cout << x << endl; else cout << w << endl; }

1

What is the output of the following segment of code if 4 is input by the user when asked to enter a number? int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: ; case 2: total = 5; case 3: total = 10; case 4: total = total + 3; case 5: total = total + 6; case 6: total = total +4; } cout << total << endl;

13

What is the value of result after the following statement executes? result = (3 * 5) % 4 + 24 / (15 - (7 - 4));

5

Which operator is used to compare two values for equality in C++?

==

The ________ decodes an instruction and generates an electronic signal.

Control Unit

Data items whose values do NOT change while the program is running are

Data items whose values do NOT change while the program is running are constants. They have fixed values throughout the program's execution.

A character literal is ________, whereas a string literal is ________

Enclosed in single quotes (' '), Enclosed in double quotes (" ")

The preprocessor executes after the compiler.

False

Which part of a for loop is executed only once at the beginning of the loop?

Initialization

What does the "cin" object in C++ primarily handle?

Input from the keyboard

What is the purpose of the "update" part in a for loop?

It modifies the loop variable to control the loop's progress

In a for loop, can you omit any of the three parts (Initialization, Condition, Update) and still have a functioning loop?

No, all three parts are required for a for loop to work

A statement that starts with a hashtag (or pound) symbol (#) is called a

Preprocessor directive

The two methods used by C++ to write computer programs are:

Procedural programming and object-oriented programming

A volatile type of memory that is used for temporary storage is

RAM (Random Access Memory)

Programs are normally stored in ________ and loaded into main memory as needed.

Secondary storage

In C++, what symbol is used to indicate the end of a statement?

Semicolon (;)

Describe the role of the "const" keyword in C++ and how it can be used with variables and functions. Provide an example of each.

The "const" keyword in C++ is used to specify that a variable or function parameter is constant, meaning its value cannot be changed. It ensures that the value remains constant throughout the program.

Explain the difference between the "if" statement and the "switch" statement in C++. When should you use one over the other?

The "if" statement is used for conditional execution, typically when you have multiple conditions to check. The "switch" statement is used for multi-way branching based on a single value, such as a menu selection.

In a for loop, what happens if the condition is always true? a) The loop code is executed once b) The loop code is never executed c) The loop code is executed indefinitely d) The loop code is executed a specific number of times

The loop code is executed indefinitely

What happens if the condition in a for loop is initially false?

The loop code is never executed

In a for loop, what does the "condition" part of the loop determine?

The number of iterations

What is the primary purpose of the "iostream" library in C++?

The primary purpose of the "iostream" library in C++ is to handle input and output operations, allowing you to read from the standard input stream (e.g., keyboard) using "cin" and write to the standard output stream (e.g., screen) using "cout."

In C++, what is the syntax for a single-line comment, and why are comments important in code?

The syntax for a single-line comment in C++ is to use double slashes (//). Comments are essential in code to provide explanations, documentation, and make code more understandable

The purpose of a memory address is:

To identify the location of a byte in memory

What is the purpose of a header file in C++?

To include preprocessor directives and function prototypes

What is the purpose of the "if" statement in C++?

To make decisions based on a condition

What is the purpose of the "break" statement in a switch statement?

To terminate the switch statement and exit the loop

A named constant is like a variable, but it its content cannot be changed while the program is running.

True

A value is stored in a variable with an assignment statement.

True

Because C++ is case-sensitive, all programs must have a function called main or Main.

True

What is the significance of variable scope in C++? Provide an example of a local and a global variable.

Variable scope in C++ defines where a variable can be accessed or used. A local variable is defined within a specific function and is only accessible within that function. A global variable is declared outside of any function and can be accessed throughout the entire program.

A variable definition tells the computer

What the variable represents or holds

Write C++ expression for the following algebraic expression: a = 3xy + 5

a = 3 * x * y + 5;

Given that x = 2, y = 1, and z = 0, what will the following cout statement display?cout << "answer = " << (x || !y && z) << endl;

answer = 1

In C++ the = operator indicates:

assignment

Declare a constant integer for minutes per hour whose value is 60

const int minutesPerHour = 60;

Rewrite the following variable definition so that the variable is a named constant. int value;

const int value = ...; // Replace "..." with the desired constant value

If you intend to place a block of statements within an if statement, you must place ________ around the block.

curly braces { }

In C++, it is impossible to display the number 34.789 in a field of 9 spaces with 2 decimal places of precision.

false

The following statement will output $5.00 to the screen: cout << setprecision(5) << dollars << endl;

false

In C++, which loop is primarily used for iterating a fixed number of times?

for loop

A debugging process where you, the programmer, pretend you are a computer and step through each statement while recording the value of each variable at each step is known as

hand tracing

Which data type should you use to store whole numbers in C++?

int

Insert the missing (blank ) parts to complete the following switch statement. int day = 2; switch ( ____ ){ ____ 1: cout << "Saturday"; break; ____ 2: cout << "Sunday"; _____ ; }

int day = 2; switch (day) { case 1: cout << "Saturday"; break; case 2: cout << "Sunday"; break; }

The modulo operator % gives us the what?

remainder

Explain static_cast<int>

static_cast is like a tool that helps you switch from one type to another. It's like saying, "I want to use this number as text, or I want to use this text as a number." It's a way to be clear about what you're doing. For example, let's say you have the number 42, but you want to use it like a decimal number, such as 42.0. You can use static_cast to tell the computer to treat it as a decimal number. Or, if you have 3.14 and want to use it as a whole number, you can use static_cast for that too. It's a helpful tool that prevents mistakes and makes sure the computer understands how you want to use your data. It's like speaking the computer's language to avoid misunderstandings.

If you place a semicolon after the statement: if (x < y)

the compiler will interpret the semicolon as a null statement

If the sub-expression on the left side of the || operator is true, the expression on the right side will not be checked.

true

Software engineering is a field that encompasses designing, writing, testing, debugging, documenting, modifying, and maintaining computer programs.

true

The CPU is the most important component in a computer because without it, the computer could not run software.

true

What is the correct syntax to declare a variable in C++?

type variableName = value;

Which loop is a better choice if you want to repeat a task until a specific condition is met and the number of iterations is unknown in advance? a) for loop b) while loop c) do-while loop d) They are equally suitable

while loop


संबंधित स्टडी सेट्स

N400 Nursing Concepts: Communication

View Set

January 20th- integumentary and intro to cartilage

View Set

GDP: Is it Counted and Where? practice

View Set