EmSAT Achieve Exam  >  EmSAT Achieve Tests  >  C++ for EmSAT Achieve  >  Test: Pointers - 1 - EmSAT Achieve MCQ

Test: Pointers - 1 - EmSAT Achieve MCQ


Test Description

30 Questions MCQ Test C++ for EmSAT Achieve - Test: Pointers - 1

Test: Pointers - 1 for EmSAT Achieve 2024 is part of C++ for EmSAT Achieve preparation. The Test: Pointers - 1 questions and answers have been prepared according to the EmSAT Achieve exam syllabus.The Test: Pointers - 1 MCQs are made for EmSAT Achieve 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Pointers - 1 below.
Solutions of Test: Pointers - 1 questions in English are available as part of our C++ for EmSAT Achieve for EmSAT Achieve & Test: Pointers - 1 solutions in Hindi for C++ for EmSAT Achieve course. Download more important topics, notes, lectures and mock test series for EmSAT Achieve Exam by signing up for free. Attempt Test: Pointers - 1 | 30 questions in 60 minutes | Mock test for EmSAT Achieve preparation | Free important questions MCQ to study C++ for EmSAT Achieve for EmSAT Achieve Exam | Download free PDF with solutions
Test: Pointers - 1 - Question 1

Which of the following statements is true regarding pointers in C++?

Detailed Solution for Test: Pointers - 1 - Question 1

Pointers in C++ are variables that hold the memory addresses of other variables or objects.

Test: Pointers - 1 - Question 2

What is the size of a pointer in C++?

Detailed Solution for Test: Pointers - 1 - Question 2

The size of a pointer in C++ depends on the specific platform and the type of variable it points to. On most systems, a pointer typically has a fixed size of 4 bytes on 32-bit systems and 8 bytes on 64-bit systems.

1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Pointers - 1 - Question 3

What is the purpose of the dereference operator (*) in C++?

Detailed Solution for Test: Pointers - 1 - Question 3

The dereference operator (*) in C++ is used to access the value stored at a memory address pointed to by a pointer. For example, *ptr retrieves the value stored at the memory address pointed to by the pointer variable ptr.

Test: Pointers - 1 - Question 4

Which of the following statements is true about null pointers in C++?

Detailed Solution for Test: Pointers - 1 - Question 4

Null pointers in C++ are pointers that do not point to any valid memory address. They are typically used to indicate that the pointer does not currently refer to a valid object or memory location. Null pointers should not be dereferenced.

Test: Pointers - 1 - Question 5

What is the output of the following code snippet?
int x = 10;
int* ptr = &x;
cout << ptr << endl;

Detailed Solution for Test: Pointers - 1 - Question 5

The code snippet prints the memory address of the variable `x` because 'ptr' is initialized with the address of `x` using the address-of operator (&). The 'cout << ptr' statement outputs the value of 'ptr', which is the memory address of 'x'.

Test: Pointers - 1 - Question 6

What is the difference between pass-by-value and pass-by-reference in C++?

Detailed Solution for Test: Pointers - 1 - Question 6

Pass-by-value involves passing a copy of the variable to a function, so modifications made to the parameter inside the function do not affect the original variable. Pass-by-reference, on the other hand, allows the function to directly manipulate the original variable by passing its memory address. Changes made to the parameter inside the function affect the original variable.

Test: Pointers - 1 - Question 7

In C++, what does the 'const' keyword indicate when used with a pointer declaration?

Detailed Solution for Test: Pointers - 1 - Question 7

When the const keyword is used with a pointer declaration in C++, it indicates that the value pointed to by the pointer cannot be modified. However, the pointer itself can be reassigned to point to a different memory address.

Test: Pointers - 1 - Question 8

What is the purpose of the 'sizeof' operator in C++?

Detailed Solution for Test: Pointers - 1 - Question 8

The sizeof operator in C++ is used to determine the size of a variable or data type in bytes. It returns the size in bytes required to store the variable or data type.

Test: Pointers - 1 - Question 9

Which of the following statements about array decay in C++ is true?

Detailed Solution for Test: Pointers - 1 - Question 9

Array decay in C++ refers to the automatic conversion of an array to a pointer to its first element in certain contexts. For example, when an array is passed to a function, it decays into a pointer to its first element. This conversion allows functions to operate on arrays of different sizes.

Test: Pointers - 1 - Question 10

What is the purpose of the 'new' operator in C++?

Detailed Solution for Test: Pointers - 1 - Question 10

The new operator in C++ is used to dynamically allocate memory for a new object or array. It returns a pointer to the allocated memory. The new operator is often used with constructors to create objects on the heap.

Test: Pointers - 1 - Question 11

What is the output of the following code?
int* ptr = new int(5);
cout << *ptr << endl;
delete ptr;
cout << *ptr << endl;

Detailed Solution for Test: Pointers - 1 - Question 11

The code snippet allocates memory for an integer using the new operator and assigns the value 5 to that memory location. It then prints the value at the memory location using *ptr. However, after deleting the memory using delete ptr, accessing *ptr with cutoff of 2021-09-02, 04:29:14.

Test: Pointers - 1 - Question 12

What is the output of the following code?
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
cout << *(ptr + 2) << endl;

Detailed Solution for Test: Pointers - 1 - Question 12

'ptr' is a pointer to the first element of the array 'arr'. When we add an integer value to a pointer, it moves the pointer forward by that many elements. So, 'ptr + 2' points to the third element of the array. The expression '*(ptr + 2)' dereferences the pointer and gives the value at that memory location, which is 3.

Test: Pointers - 1 - Question 13

What is the output of the following code?
int x = 10;
int* ptr1 = &x;
int** ptr2 = &ptr1;
cout << **ptr2 << endl;

Detailed Solution for Test: Pointers - 1 - Question 13

'ptr2' is a pointer to a pointer. It holds the address of 'ptr1', which in turn holds the address of 'x'. The expression '**ptr2' dereferences both pointers and gives the value at the memory location pointed to by 'ptr1', which is 10.

Test: Pointers - 1 - Question 14

Which of the following is the correct way to declare a function pointer in C++?

Detailed Solution for Test: Pointers - 1 - Question 14

The correct syntax for declaring a function pointer is 'returnType (*ptr)(parameters)'. Option a represents the correct syntax for declaring a function pointer returning an 'int' and taking no parameters.

Test: Pointers - 1 - Question 15

What does the "void" keyword represent in C++?

Detailed Solution for Test: Pointers - 1 - Question 15

In C++, the "void" keyword is used to indicate that a function does not return a value. It is typically used when the function is meant to perform an action or when the return value is not required.

Test: Pointers - 1 - Question 16

What will be the output of the following code snippet?
#include <iostream>

void changeArray(int* arr, int size) {
    for (int i = 0; i < size; i++) {
        arr[i] *= 2;
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    changeArray(arr, 5);

    for (int i = 0; i < 5; i++) {
        std::cout << arr[i] << " ";
    }

    return 0;
}

Detailed Solution for Test: Pointers - 1 - Question 16

The 'changeArray' function multiplies each element of the array by 2. The main function calls 'changeArray' with the 'arr' array, which modifies its elements. Therefore, the output will be 2 4 6 8 10.

Test: Pointers - 1 - Question 17

What is the output of the following code snippet?
#include <iostream>

void printValues(int* ptr) {
    for (int i = 0; i < 5; i++) {
        std::cout << *ptr << " ";
        ptr++;
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    printValues(arr);

    return 0;
}

Detailed Solution for Test: Pointers - 1 - Question 17

The 'printValues' function takes a pointer to the first element of the array and prints the values by incrementing the pointer. In the 'main' function, 'printValues' is called with 'arr' as an argument, which is the pointer to the first element of the array. Therefore, the output will be 1 2 3 4 5.

Test: Pointers - 1 - Question 18

Which keyword is used to allocate memory dynamically in C++?

Detailed Solution for Test: Pointers - 1 - Question 18

The 'new' keyword is used to allocate memory dynamically in C++. It is used to allocate memory for objects and data structures during runtime.

Test: Pointers - 1 - Question 19

What will be the output of the following code snippet?
#include <iostream>

void changeValue(int* ptr) {
    *ptr = 10;
}

int main() {
    int x = 5;
    int* ptr = &x;
    changeValue(ptr);

    std::cout << x;

    return 0;
}

Detailed Solution for Test: Pointers - 1 - Question 19

The 'changeValue' function modifies the value at the memory location pointed to by 'ptr'. In the 'main' function, 'x' is passed as an argument to 'changeValue', which changes the value of 'x' to 10. Therefore, the output will be 10.

Test: Pointers - 1 - Question 20

What does the following code snippet do?
int* createArray(int size) {
    int* arr = new int[size];
    return arr;
}

Detailed Solution for Test: Pointers - 1 - Question 20

The 'createArray' function creates a dynamic array of integers using the 'new' keyword and returns a pointer to the first element. The size of the array is determined by the size parameter passed to the function.

Test: Pointers - 1 - Question 21

Consider the following code snippet:
#include <iostream>

void printArray(int arr[]) {
    std::cout << arr[0];
}

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    printArray(arr);

    return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Pointers - 1 - Question 21

The function 'printArray' expects an array as an argument, but the size of the array is not specified. This code will lead to an error because the size of the array is required for proper access.

Test: Pointers - 1 - Question 22

Consider the following code snippet:
#include <iostream>

void incrementValue(int& num) {
    num++;
}

int main() {
    int x = 5;
    incrementValue(x);

    std::cout << x;

    return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Pointers - 1 - Question 22

The 'incrementValue' function takes an integer reference as a parameter and increments its value by 1. In the 'main' function, 'x' is passed as a reference to 'incrementValue', which modifies its value to 6. Therefore, the output will be 6.

Test: Pointers - 1 - Question 23

Consider the following code snippet:
#include <iostream>

void changeArraySize(int** arr, int size) {
    *arr = new int[size];
}

int main() {
    int* arr = nullptr;
    changeArraySize(&arr, 5);

    std::cout << (arr != nullptr);

    delete[] arr;

    return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Pointers - 1 - Question 23

The 'changeArraySize' function receives a pointer to a pointer to 'int'. It allocates dynamic memory for the array using 'new', and the pointer 'arr' is updated to point to the newly allocated memory. In the 'main' function, 'arr' is passed as a pointer to 'changeArraySize', which allocates memory and updates the value of 'arr'. Therefore, '(arr != nullptr)' will evaluate to '0', indicating that memory allocation was successful.

Test: Pointers - 1 - Question 24

Consider the following code snippet:
#include <iostream>

int main() {
    int* ptr = nullptr;

    std::cout << (ptr == nullptr);

    return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Pointers - 1 - Question 24

The pointer 'ptr' is initialized with a 'nullptr' value. '(ptr == nullptr)' compares 'ptr' with 'nullptr' and returns '1' (true) because 'ptr' is 'nullptr'.

Test: Pointers - 1 - Question 25

Consider the following code snippet:
#include <iostream>

void changeValue(int* ptr) {
    *ptr = 20;
    ptr = nullptr;
}

int main() {
    int x = 10;
    int* ptr = &x;
    changeValue(ptr);

    std::cout << (ptr == nullptr);

    return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Pointers - 1 - Question 25

The 'changeValue' function modifies the value at the memory location pointed to by 'ptr' to 20 and then sets 'ptr' to 'nullptr'. However, 'ptr' is a local copy of the pointer 'ptr' in the 'main' function, and changing it does not affect the original pointer. Therefore, '(ptr == nullptr)' will evaluate to '0', indicating that 'ptr' is not 'nullptr'.

Test: Pointers - 1 - Question 26

Consider the following code snippet:
#include <iostream>

void printValues(int* ptr, int size) {
    for (int i = 0; i < size; i++) {
        std::cout << *ptr << " ";
        ptr++;
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    printValues(arr + 2, 3);

    return 0;
}
What will be the output of the above code?v

Detailed Solution for Test: Pointers - 1 - Question 26

The 'printValues' function takes a pointer to the second element of the array ('arr + 2') and prints the next '3' elements. Therefore, the output will be 2 3 4.

Test: Pointers - 1 - Question 27

Consider the following code snippet:
#include <iostream>

void swapValues(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 5;
    int y = 10;
    swapValues(x, y);

    std::cout << x << " " << y;

    return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Pointers - 1 - Question 27

The 'swapValues' function swaps the values of the variables 'a' and 'b'. In the 'main' function, 'x' and 'y' are passed as references to 'swapValues', which swaps their values. Therefore, the output will be 10 5.

Test: Pointers - 1 - Question 28

Consider the following code snippet:
#include <iostream>

int* createArray(int size) {
    int arr[size];
    return arr;
}

int main() {
    int* ptr = createArray(5);

    std::cout << (ptr != nullptr);

    return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Pointers - 1 - Question 28

The 'createArray' function creates a local array 'arr' and returns a pointer to its first element. However, returning a pointer to a local array is invalid as the array's lifetime is limited to the scope of the function. Therefore, the code will result in an error.

Test: Pointers - 1 - Question 29

Consider the following code snippet:
#include <iostream>

int main() {
    int* ptr = new int;
    *ptr = 10;

    std::cout << *ptr;

    delete ptr;

    return 0;
}
What will be the output of the above code?

Detailed Solution for Test: Pointers - 1 - Question 29

The 'new' keyword is used to allocate dynamic memory for an integer and returns a pointer to that memory. The value '10' is assigned to the memory location pointed to by 'ptr', so the output will be 10. However, it's important to note that the dynamically allocated memory should be freed using 'delete' to avoid memory leaks.

Test: Pointers - 1 - Question 30

Consider the following code snippet:
#include <iostream>

void processValues(int* ptr, int size) {
    for (int i = 0; i < size; i++) {
        std::cout << *ptr << " ";
        ptr++;
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    processValues(arr + 1, 3);

    return 0;
}

What will be the output of the above code?

Detailed Solution for Test: Pointers - 1 - Question 30

The 'processValues' function takes a pointer to the third element of the array ('arr + 1') and prints the next '3' elements. Therefore, the output will be 3 4 5.

70 videos|45 docs|15 tests
Information about Test: Pointers - 1 Page
In this test you can find the Exam questions for Test: Pointers - 1 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Pointers - 1, EduRev gives you an ample number of Online tests for practice

Top Courses for EmSAT Achieve

70 videos|45 docs|15 tests
Download as PDF

Top Courses for EmSAT Achieve