Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

C++ Programming Multiple Choice Questions (MCQ)

Total questions: 36

Worksheet time: 18mins

Name
Class
Date
1.

Apa header file yang paling umum digunakan untuk operasi input/output dasar (seperti std::cout dan std::cin) dalam program C++?

a)

b)

c)

d)

2.

Apa nilai akhir dari variabel x setelah eksekusi kode berikut: int x = 5; x++;?

a)

A. 4

b)

B. 5

c)

C. 6

d)

D. 7

3.

Tipe data manakah yang paling cocok untuk menyimpan bilangan bulat yang tidak pernah negatif (misalnya, jumlah item dalam keranjang)?

a)

int

b)

unsigned int

c)

float

d)

double

4.

Dalam C++, jika int* ptr; dideklarasikan, simbol operator manakah yang digunakan untuk mengambil nilai yang disimpan di alamat memori yang ditunjuk oleh ptr?

a)

& (Address-of)

b)

* (Dereference/Indirection)

c)

-> (Arrow)

d)

. (Dot)

5.

Pernyataan kontrol manakah yang menjamin bahwa blok kode akan dieksekusi setidaknya satu kali?

a)

while loop

b)

for loop

c)

if-else statement

d)

do-while loop

6.

Apa istilah yang digunakan untuk mendefinisikan beberapa fungsi dengan nama yang sama tetapi dengan tipe dan/atau jumlah parameter yang berbeda?

a)

Function Overriding

b)

Polymorphism

7.

In the context of OOP C++, what is the role of the keyword 'private' for a member (variable or function) of a class?

a)

Allows access from anywhere in the program.

b)

Only allows access by member functions of the class itself.

c)

Allows access by derived classes.

d)

Makes the member static (fixed).

8.

If you declare an array as int data[5];, what is the last valid index for that array?

a)

A. 4

b)

B. 5

c)

C. 6

d)

D. 0

9.

Which operator is used to allocate memory dynamically from the heap at runtime in C++?

a)

malloc

b)

free

c)

new

d)

delete

10.

Metode manakah yang paling tepat digunakan untuk membaca string dari input yang mungkin mengandung spasi?

a)

std::cin >> myString;

b)

std::cin.get(myString);

c)

std::getline(std::cin, myString);

d)

std::cin.read(myString);

11.

Apa hasil dari ekspresi berikut: 7 % 3?

a)

A. 2

b)

B. 1

c)

C. 0

d)

D. 3

12.

Operator mana yang dikenal sebagai operator scope resolution dalam C++?

a)

::

b)

->

c)

.

d)

*

13.

Perbedaan utama antara operator pre-increment (++x) dan post-increment (x++) adalah...

a)

Pre-increment menambah nilai sebelum digunakan, sedangkan post-increment menambah nilai setelah digunakan.

b)

Pre-increment mengurangi nilai sebelum digunakan, sedangkan post-increment mengurangi nilai setelah digunakan.

c)

Pre-increment hanya dapat digunakan pada variabel integer, sedangkan post-increment pada variabel float.

d)

Pre-increment digunakan untuk operasi logika, sedangkan post-increment untuk operasi aritmatika.

14.

Tipe data apa yang digunakan untuk menyimpan nilai benar (true) atau salah (false)?

a)

char

b)

void

c)

bool

d)

long

15.

Bagaimana Anda mendeklarasikan variabel x sebagai konstanta dengan nilai 10?

a)

int const x = 10;

b)

static int x = 10;

c)

volatile int x = 10;

d)

final int x = 10;

16.

Jika Anda melewati array ke fungsi dalam C++, secara default ia diteruskan dengan cara apa?

a)

By Value (Lewat Nilai)

b)

By Reference (Lewat Referensi)

c)

By Copy (Lewat Salinan)

d)

By Pointer (Lewat Pointer)

17.

Apa yang akan menjadi output dari kode berikut? C++ int arr[] = {1, 2, 3}; std::cout << *(arr + 1);

a)

1

b)

2

c)

3

d)

Alamat memori

18.

Apa yang terjadi jika Anda mendeklarasikan fungsi tanpa menentukan tipe kembalian (return type) di C++ (sebelum standar C++17)?

a)

Kompiler akan memberikan error.

b)

Kompiler secara default menganggap tipe kembaliannya adalah void.

c)

Kompiler secara default menganggap tipe kembaliannya adalah int.

d)

Kompiler secara default menganggap tipe kembaliannya adalah char.

19.

Fungsi yang tidak mengembalikan nilai apa pun harus dideklarasikan dengan tipe kembalian apa?

a)

null

b)

empty

c)

void

d)

nothing

20.

When a local variable is declared in a function, where is the memory for the variable usually allocated?

a)

Heap

b)

Stack

c)

Static Memory

d)

Register

21.

Which of the following switch statements will execute the statement in the next case regardless of its condition?

a)

break;

b)

continue;

c)

return;

d)

No statement is used (fall-through).

22.

How many times will the following loop execute? C++ for (int i = 0; i < 5; i++);

a)

A. 4

b)

B. 5

c)

C. 6

d)

D. 0 (The loop does nothing)

23.

Which keyword is used to stop the current loop iteration and continue to the next iteration?

a)

A. break

b)

B. return

c)

C. continue

d)

D. goto

24.

Consider this code: C++ int x = 10; if (x > 10) std::cout << "A"; else if (x < 10) std::cout << "B"; else std::cout << "C"; What is the output of this code?

a)

A. A

b)

B. B

c)

C. C

d)

D. Error

25.

In a range-based for loop (for example, for (int val : myArr)), val is a copy of the array element. How do you modify the original array element inside the loop?

a)

Using val*

b)

Using reference: for (int &val : myArr)

c)

Using pointer: for (int *val : myArr)

26.

What is called an instance of a class?

a)

Template

b)

Object

c)

Function

d)

Structure

27.

Which special member function is called automatically when an object is created?

a)

Destructor

b)

Mutator

c)

Constructor

d)

Accessor

28.

Which accessibility allows class members to be accessed by the class itself and also by derived classes, but not from outside the hierarchy?

a)

public

b)

private

c)

protected

d)

static

29.

In the concept of Polymorphism, what is the term for creating a function in a derived class that has the exact same name and signature as the function in the base class?

a)

Overriding

b)

Overloading

c)

Inheritance

d)

Encapsulation

30.

Which of the following refers to the concept of redefining a function in a derived class with the same signature as in the base class?

a)

Function Overloading

b)

Function Hiding

c)

Function Overriding

d)

Function Binding

31.

What is the main benefit of using a Destructor (~ClassName())?

a)

Initializing the initial value of the object.

b)

Performing dynamic memory allocation.

c)

Cleaning up resources (e.g., releasing heap memory) before the object is destroyed.

d)

Making the object constant.

32.

If you allocate memory using new in C++, which operator should you use to release a dynamic memory array (not a single object)?

a)

delete ptr;

b)

delete [] ptr;

c)

free(ptr);

d)

delete array;

33.

In the context of std::string, which method is used to get the length of a string?

a)

A. size() or length()

b)

B. count()

c)

C. sizeof()

34.

Kapan memory leak terjadi dalam program C++?

a)

Ketika terlalu banyak variabel dideklarasikan.

b)

Ketika Anda mengalokasikan memori menggunakan new tetapi lupa melepaskannya dengan delete.

c)

Ketika program menggunakan seluruh memori stack.

d)

Ketika menggunakan pass-by-value untuk objek besar.

35.

Apa output dari kode berikut: std::cout << "5" + "5";?

a)

10

b)

55

c)

Error kompilasi/perilaku yang tidak terdefinisi

d)

5

36.

Apa yang ditunjuk oleh pointer this di dalam fungsi anggota non-statik sebuah class C++?

a)

Alamat objek base class.

b)

Alamat objek derived class.

c)

Alamat objek saat ini (current object) di mana fungsi dipanggil.

d)

Alamat fungsi anggota.