0% found this document useful (0 votes)
7 views3 pages

Initial Values: Step 1: A (3) ++

Uploaded by

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

Initial Values: Step 1: A (3) ++

Uploaded by

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

After the following code has been executed, what values will the array a hold?

int[] a = {1, 2, 3, 4, 5,
6, 7 }; a[3]++; a[2] = a[3] + 1; a[6]++; a[5] = a[2] / 2;

Let's break down the code step by step:

Initial values: a = {1, 2, 3, 4, 5, 6, 7}

Step 1: a[3]++

 a[3] (the 4th element) is incremented by 1.


New value: a[3] = 4 + 1 = 5.
Updated array: a = {1, 2, 3, 5, 5, 6, 7}.

Step 2: a[2] = a[3] + 1

 a[3] (value is now 5) is added to 1 and assigned to a[2].


New value: a[2] = 5 + 1 = 6.
Updated array: a = {1, 2, 6, 5, 5, 6, 7}.

Step 3: a[6]++

 a[6] (the 7th element) is incremented by 1.


New value: a[6] = 7 + 1 = 8.
Updated array: a = {1, 2, 6, 5, 5, 6, 8}.

Step 4: a[5] = a[2] / 2

 a[2] (value is 6) is divided by 2, and the integer result is assigned to a[5].


New value: a[5] = 6 / 2 = 3.
Final array: a = {1, 2, 6, 5, 5, 3, 8}.

Final Values of the Array:

a = {1, 2, 6, 5, 5, 3, 8}

1. What is a 1-dimensional array?

a) A list of lists.
b) A collection of elements stored in rows and columns.
c) A linear collection of elements stored in contiguous memory locations.
d) A data structure with only one element.

Answer: c) A linear collection of elements stored in contiguous memory locations.

2. How do you declare a 1-dimensional array in C?

a) int arr[5];
b) array<int> arr[5];
c) arr[5] int;
d) int array(5);
Answer: a) int arr[5];

3. What is the index of the first element in an array in most programming languages?

a) 0
b) 1
c) -1
d) It depends on the language.

Answer: a) 0

4. What will be the output of the following code?

Copy code

int a[5] = {1, 2, 3, 4, 5};

printf("%d", a[2]);

a) 1
b) 2
c) 3
d) 4

Answer: c) 3

5. What happens if you access an index outside the bounds of an array?

a) The program will compile but cause undefined behavior at runtime.


b) The compiler will show an error.
c) The program will always crash.
d) The program will return a default value.

Answer: a) The program will compile but cause undefined behavior at runtime.

6. What is the correct way to initialize a 1D array of size 4 in Java?

a) int[] arr = {1, 2, 3, 4};


b) int arr = new int[4];
c) int arr[4] = {1, 2, 3, 4};
d) int arr(4) = {1, 2, 3, 4};

Answer: a) int[] arr = {1, 2, 3, 4};

7. If you want to access the last element of an array arr in Python, which index should you use?
a) 0
b) -1
c) len(arr)
d) arr.size - 1

Answer: b) -1

8. What does the following code print?

Copy code

int a[3] = {1, 2, 3};

printf("%d", a[3]);

a) 3
b) 0
c) Undefined behavior
d) Compiler error

Answer: c) Undefined behavior

9. How do you find the size of an array in C?

a) sizeof(arr)
b) sizeof(arr) / sizeof(arr[0])
c) arr.size()
d) length(arr)

Answer: b) sizeof(arr) / sizeof(arr[0])

10. Which of the following is true about arrays?

a) Arrays can have elements of different data types.


b) The size of an array can be changed after initialization.
c) Arrays store elements in contiguous memory locations.
d) Arrays in C start indexing from 1.

Answer: c) Arrays store elements in contiguous memory locations.

You might also like