Search Header Logo
Advanced C++ Concepts

Advanced C++ Concepts

Assessment

Presentation

Computers

University

Practice Problem

Medium

Created by

Dr. Moriarty

Used 2+ times

FREE Resource

6 Slides • 14 Questions

1

Pass by value vs reference

​In C++, pass by value means a function receives a copy of the argument, so changes made to the parameter do not affect the original variable.


Pass by reference allows the function to work directly with the original variable by using its memory address, so changes made inside the function are reflected in the original variable outside the function.

2

Multiple Choice

Question image

What will this code print?

1

After modifyValue: 20, After modifyReference: 30

2

After modifyValue: 10, After modifyReference: 30

3

After modifyValue: 20, After modifyReference: 10

4

After modifyValue: 10, After modifyReference: 10

3

Multiple Choice

Question image

What will this code print?

1

After doubleValue: 10, After doubleReference: 20

2

After doubleValue: 5, After doubleReference: 10

3

After doubleValue: 5, After doubleReference: 5

4

After doubleValue: 10, After doubleReference: 10

4

Static Variables

In C++, a static variable inside a function retains its value between function calls, unlike a regular local variable that is re-initialized each time the function is called. This allows the variable to maintain state across multiple invocations of the function, effectively acting like a persistent, function-level global variable. Static variables are initialized only once and keep their value throughout the program's lifetime.

5

Multiple Choice

Question image

What will this code print?

1

Counter: 1, Counter: 1, Counter: 1

2

Counter: 0, Counter: 1, Counter: 2

3

Counter: 1, Counter: 2, Counter: 3

4

Counter: 1, Counter: 2, Counter: 2

6

Multiple Choice

Question image

What will this code print?

1

Num: 5, Num: 5, Num: 5, Num: 5

2

Num: 5, Num: 10, Num: 15, Num: 20

3

Num: 10, Num: 15, Num: 20, Num: 25

4

Num: 10, Num: 20, Num: 30, Num: 40

7

Default Parameters

In C++, default parameters allow you to specify a default value for a function parameter so that if the argument is omitted during a function call, the default value is used. This makes functions more flexible by allowing them to be called with fewer arguments. Default parameters must be defined from right to left in the function signature to avoid ambiguity.

8

Multiple Choice

Question image

What will this code print?

1

Hello, Alice!
Hello, Alice!

2

Hello, Alice!
Hello, Guest!

3

Hello, Guest!
Hello, Guest!

4

The code will not compile due to missing parameters.

9

Multiple Choice

Question image

What will this code print?

1

a: 5, b: 10, c: 20
a: 7, b: 15, c: 20
a: 3, b: 8, c: 20

2

a: 5, b: 10, c: 20
a: 7, b: 15, c: 20
a: 3, b: 8, c: 12

3

a: 5, b: 10, c: 20
a: 7, b: 10, c: 20
a: 3, b: 8, c: 20

4

The code will not compile due to incorrect default parameter usage.

10

Template Functions

In C++, template functions allow you to write a single function that can work with different data types, making code more reusable and flexible.

By defining a function template with a type parameter (e.g., template<typename T>), the compiler can generate a version of the function for each specific type used during a function call. This feature is particularly useful for creating generic algorithms that work with any data type.

11

Multiple Choice

Question image

What will this code print?

1

5 5
3.14 3.14
Hello Hello

2

5 5
3.14 3.14
Compilation error due to using a string literal

3

5 5
3.14 3.14
HelloHello

4

The code will not compile due to incorrect template usage.

12

Multiple Choice

Which of the following statements about template functions in C++ is true?

1

Template functions must be defined for each specific data type before they are used.

2

Template functions can only accept one type parameter.

3

Template functions allow a single function definition to work with multiple data types.

4

Template functions cannot be used with built-in data types like int or double.

13

Multiple Choice

Question image

Extra Credit: Who is this Zelda character?

1

Tingle

2

Gannon

3

Beedle

4

Sephiroth

14

Inline Functions

In C++, inline functions are functions that suggest to the compiler to replace the function call with the actual code of the function to reduce the overhead of function calls.

This can improve performance for small, frequently called functions, but the compiler may ignore the inline suggestion for complex functions.

Declaring a function as inline does not guarantee that it will be inlined, as this decision is ultimately up to the compiler.

15

Multiple Choice

Question image

What will this print?

1

Square of 4 is: 4
Square of 5 is: 5

2

Square of 4 is: 16
Square of 5 is: 25

3

Square of 4 is: 16
Square of 5 is: 20

4

The code will not compile due to inline function usage.

16

Multiple Choice

Which of the following statements about inline functions in C++ is true?

1

Inline functions are guaranteed to be inlined by the compiler.

2

Inline functions help reduce the overhead of function calls by suggesting the compiler to replace the function call with its body.

3

Inline functions can only be defined within the class declaration.

4

Inline functions cannot include loops or complex logic.

17

Constants in C++

in C++ const correctness ensures that variables, function parameters, and return types that should not be modified are declared as const. This helps prevent unintended changes to data, making code safer and easier to maintain. Using const can also improve code readability by clearly indicating which values are intended to remain constant.

18

Multiple Choice

Question image

what will this code print?

1

5

2

6

3

nothing

4

compile error

19

Multiple Choice

Which of the following statements about const correctness in C++ is true?

1

A const parameter passed by reference allows the function to modify the original argument.

2

Declaring a function parameter as const ensures that the function cannot modify the parameter within its body.

3

Using const with a function return type guarantees that the function's return value cannot be modified by the caller.

4

A const variable can be modified after its initial declaration as long as it is within the same function.

20

Multiple Choice

Question image

Extra Credit: What Mega Man boss is this?

1

Drill Man

2

Metal Man

3

Crash Man

4

Waluigi

Pass by value vs reference

​In C++, pass by value means a function receives a copy of the argument, so changes made to the parameter do not affect the original variable.


Pass by reference allows the function to work directly with the original variable by using its memory address, so changes made inside the function are reflected in the original variable outside the function.

Show answer

Auto Play

Slide 1 / 20

SLIDE