Search Header Logo

chapter 2 Variable Declaration and Scope Quiz2

Authored by MAHANI BINTI ZAKARIA (POLIMAS)

Information Technology (IT)

University

Used 14+ times

chapter 2 Variable Declaration and Scope Quiz2
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

5 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What will happen if you try to use a variable that is not declared?

It will be created automatically

Syntax error

Assigned value 0

Stored as a constant

Answer explanation

In C++, all variables must be declared before they are used. If you try to use a variable without declaring it, the compiler will give an error because it doesn’t know the variable’s type or memory location.

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following is a correct way to declare and initialize a float variable?

float = 3.14;

float pi = 3.14;

3.14 = float pi;

declare float pi;

Answer explanation

The correct way to declare and initialize a float is by first stating the data type (float), then the variable name (pi), and then assigning a value (3.14). The other options use incorrect syntax.

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What does variable scope refer to?

The data type of the variable

The location where the variable can be accessed

The name of the variable

The size of memory used

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the difference between a local and a global variable?

Local variables are always integers

Global variables are only accessible in loops

Local variables are declared inside functions, global outside all functions

Global variables are deleted immediately after use

Answer explanation

Scope means where in the program you can use the variable. For example, a variable declared inside a function can only be used inside that function (local scope).

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Media Image

What is the output of this code?

50

25

0

Error

Answer explanation

The num inside main() is a local variable with a value of 25. It hides (overrides) the global num with a value of 50. So, the program prints 25.

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?