This lab differs from the previous ones as there are no new programming exercises. It is intended to give you an opportunity to go back over the previous weeks lab exercises and to complete those questions you were unable to finish previously. This may include questions where you have an answer, but for which you harbor suspicions that your solution only works in particular limited cases or might be improved in some way.
If youve found the questions to be easy so far, then use this lab to write radically new programs as answers to the most interesting of the questions. For example, if you used a loop in your previous answer, try to do it with a recursive function call instead.
So what exactly is this lab?
Your lab time is best spent getting help on the trickiest questions in the previous labs:
Here are some questions that are intended as practice questions for the quizzes and midterm exams. Try to do them without using a computer or any external help, but then carefully check your answers using g++.
Question 1
#include <iostream>using namespace std; void func-x(int p[4]){ int i=10; p=&i; cout << p[0]} int main(){ int arr[4] = {1, 2, 3, 4} func-x(arr); cout << arr[0]; return 0;}
What will be the output of the above program?
- 10 10
- Compiler Error
- 10 1
- Undefined Behavior
- 1 10
Question 2
#include <iostream>using namespace std; int main(){ int arr[4] = {1, 2, 3, 4}; int p[4]; p=arr; cout << p[1];}
What will be the output?
- 1
- Compile time error
- Undefined Behavior
- 2
- {2, 3, 4}
Question 3
Which of the following is an illegal declaration?
- int a=0, b = 1, c=2; int array[3] = {a, b, c}
- int size = 3; int array[size];
- int size = 3; int array[size] = {1, 2, 3};
- int array[3] = {1, 2, 3, 4};
- All of the above
Question 4
#include <iostream>using namespace std; int main(){ int arr[5] = {1,2,3,4,5}; int i = -1; cout << arr[5]; return 0;}
What will be the output of the above program?
- Garbage/undefined value
- 5
- 6
- 0
- None of the above
Question 5
#include <iostream>using namespace std; int main(){ int a[5] = {51, 1, 5, 20, 25}; int x, y, z; x = ++a[1]; y = a[1]++; z = a[x++]; cout << x << << y << << z; return 0;}
What will be the output of the above program?
- 2, 3, 20
- 2, 1, 5
- 2, 2, 5
- 1, 2, 5
- 3, 2, 5
Question 6
An arrays elements are always stored in ________ memory locations.
- Sequential
- Random
- Sequential and Random
- Sometimes sequential and sometimes random
- None of the above
Question 7
#include <iostream>using namespace std; int main(){ char str[5] = XYZ; cout << str[3]; return 0;}
What will be the output of the above program?
- XYZ
- Nothing will be printed
- Z
- 0