0% found this document useful (0 votes)
3 views8 pages

Pointer 01

The document contains a series of questions and answers about C programming, specifically focusing on pointer usage and memory manipulation. Each question presents a code snippet, followed by multiple-choice options and an explanation of the correct answer. The correct answers highlight the behavior of pointers, dereferencing, and function calls that modify variable values.

Uploaded by

anushya.himate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Pointer 01

The document contains a series of questions and answers about C programming, specifically focusing on pointer usage and memory manipulation. Each question presents a code snippet, followed by multiple-choice options and an explanation of the correct answer. The correct answers highlight the behavior of pointers, dereferencing, and function calls that modify variable values.

Uploaded by

anushya.himate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Question 1

What will be the output of the following C code?


#include <stdio.h>

int main() {
int a = 5;
int *p = &a;
printf("%d", *p);
return 0;
}

Options:
a) 5
b) Address of a
c) 0
d) Compilation error
—------------------------------------------------------------------------------
Correct Answer: a) 5
Explanation: The pointer p points to a, and dereferencing p with *p retrieves the value
of a, which is 5.
Question 2
What will be the output of the following C code?
#include <stdio.h>

int main() {
int a = 10;
int *p = &a;
*p = 20;
printf("%d", a);
return 0;
}

Options:
a) 10
b) 20
c) Compilation error
d) Undefined behavior
—------------------------------------------------------------------------------------------
Correct Answer: b) 20
Explanation: The pointer p is used to modify the value of a through dereferencing.
After executing *p = 20;, the value of a becomes 20.
Question 3
What will be the output of the following C code?

#include <stdio.h>

int main() {
int a = 15;
int *p = &a;
printf("%d %d", a, *p);
return 0;
}

Options:
a) 15 15
b) 15 Address of a
c) Compilation error
d) Undefined behavior

—-------------------------------------------------------------------------------------------

Correct Answer: a) 15 15
Explanation: Both a and *p point to the same value, which is 15, so the output will be
15 15.
Question 4
What will be the output of the following C code?

#include <stdio.h>

int main() {
int a = 30;
int *p = &a;
printf("%d", *(&(*p)));
return 0;
}

Options:
a) Address of a
b) Compilation error
c) 30
d) Undefined behavior
—------------------------------------------------------------------------------------------------
Correct Answer: c) 30
Explanation: The expression *(&(*p)) dereferences p to get a, and then takes its
address and dereferences it again, resulting in 30.
Question 5
What will be the output of the following C code?

#include <stdio.h>

void updateValue(int *x) {


*x += 10;
}

int main() {
int a = 5;
updateValue(&a);
printf("%d", a);
return 0;
}

Options:
a) 5
b) 10
c) 15
d) Compilation error

—----------------------------------------------------------------------------------------
Correct Answer: c) 15
Explanation: The function updateValue modifies the value of a by adding 10, so after
calling it, a becomes 15.
Question 6
What will be the output of the following C code?

#include <stdio.h>

int main() {
int a = 1, b = 2;
int *p1 = &a, *p2 = &b;

printf("%d %d", (*p1 + *p2), (*p1 * *p2));


return 0;
}

Options:
a) 3 2
b) 3 4
c) Compilation error
d) Undefined behavior
—-------------------------------------------------------------------------------------
Correct Answer: b) 3 2
Explanation: The first part calculates the sum (1 + 2) which is 3, and the second part
calculates the product (1 * 2) which is 2, resulting in 3 2.
Question 7
What will be the output of the following C code?

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3};
int *ptr = arr;

printf("%d %d", *(ptr + 1), ptr[2]);


return 0;
}

Options:
a) Compilation error
b) Undefined behavior
c) 2,3
d) Access violation
—------------------------------------------------------------------------------------------------
Correct Answer: c) 2,3
Explanation: The expression *(ptr + 1) accesses the second element (value 2) and
ptr accesses the third element (value 3) of the array.
Question 8
What will be the output of this C code?

#include <stdio.h>

void swap(int *x, int *y) {


int temp = *x;
*x = *y;
*y = temp;
}

int main() {
int a = 10, b = 20;
swap(&a, &b);
printf("%d %d", a, b);
return 0;
}

Options:
a) Compilation error
b) Runtime error
c) "10" "20"
d) "20" "10"
—-------------------------------------------------------------------------------
Correct Answer: d) "20" "10"
Explanation: The function swap exchanges the values of a and b, so after calling it, a
becomes 20, and b becomes 10, resulting in output "20" "10".

You might also like