0% found this document useful (0 votes)
18 views17 pages

C/C++/Java Output and Error Predictions

The document contains a series of programming questions related to C, C++, and Java, focusing on concepts such as pointer arithmetic, array indexing, string manipulation, and error identification. Each question is followed by multiple-choice answers, with the correct answers provided at the end. The questions test knowledge on syntax, behavior of code snippets, and debugging skills.

Uploaded by

Nikhil Patel
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)
18 views17 pages

C/C++/Java Output and Error Predictions

The document contains a series of programming questions related to C, C++, and Java, focusing on concepts such as pointer arithmetic, array indexing, string manipulation, and error identification. Each question is followed by multiple-choice answers, with the correct answers provided at the end. The questions test knowledge on syntax, behavior of code snippets, and debugging skills.

Uploaded by

Nikhil Patel
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

Q1: Output of this Pointer Code

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4};
int *p = arr;
printf("%d %d", *(p+1), *(p+2));
return 0;
}

Predict the output.

A) 1 2​
B) 2 3​
C) 3 4​
D) Compilation Error

Q2: Output of This Array Code

#include <stdio.h>

int main() {

int arr[] = {10, 20, 30, 40};

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

return 0;

A) 30​
B) 20​
C) Compilation Error​
D) Undefined Behavior

Q3: Pointer Arithmetic Output

#include <stdio.h>

int main() {

int arr[] = {5, 10, 15};

int *p = arr;

printf("%d", *(p + 1));

return 0;

A) 5​
B) 10​
C) 15​
D) Compilation Error

Q4: Character Array vs. String Literal Behavior

#include <stdio.h>

int main() {

char str1[] = "Hello";


char *str2 = "World";

str1[0] = 'h';

str2[0] = 'w';

printf("%s %s", str1, str2);

return 0;

A) hello world​
B) Segmentation Fault​
C) Compilation Error​
D) Undefined Behavior

Q5: Post-Increment in Array Indexing

#include <stdio.h>

int main() {

int arr[] = {10, 20, 30, 40};

int i = 0;

printf("%d %d", arr[i++], arr[i]);

return 0;
}

A) 10 20​
B) 10 10​
C) Compilation Error​
D) Undefined Behavior

Q6. Find the error in this C++ program:


#include <iostream>
using namespace std;
int main() {
char str[5] = "Hello";
cout << str;
}

Options:​
A) String is too large for char[5]​
B) str should be char str[]​
C) str should be char str[6]​
D) No error

Q7. What will be the output of this code?

#include <iostream>

using namespace std;

int main() {

char str[] = "Hello";

cout << *(str + 1);

Options:​
A) H​
B) e​
C) l​
D) o

Q8. Find the error in the Java code:

class Test {

public static void main(String args) {

[Link]("Hello");

Options:​
A) No error​
B) main method should take String[] args​
C) Syntax error in println​
D) System class cannot be used

Q9. What is wrong with this Java program?

class Test {

public static void main(String[] args) {

int x = 10;

[Link](X);

Options:​
A) X is undefined (case-sensitive)​
B) main method is incorrect​
C) [Link] is incorrect​
D) No error

Q10. What is the issue in this Java program?

class Test {

public static void main(String[] args) {

int arr[] = new int[5];

[Link](arr[5]);

Options:​
A) No error​
B) Array index out of bounds at runtime​
C) [Link] should be inside a loop​
D) arr must be initialized with values

Q11. What is the problem in this Java program?

class Test {

public static void main(String[] args) {

int[] arr = new int[5];

[Link](arr[-1]);

Options:​
A) No error​
B) Negative index access leads to ArrayIndexOutOfBoundsException​
C) arr[-1] returns default value 0​
D) arr must be initialized with values

Q12. What is the issue in this Java code?

class Test {

public static void main(String[] args) {

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

for (int i : arr) {

if (i == 3) continue;

[Link](i);

Options:​
A) No error​
B) continue cannot be used in an enhanced for-loop​
C) break should be used instead of continue​
D) for loop should use i = 0; i < [Link]; i++

Q13. What is the output of this program?

#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40};
cout << sizeof(arr) / sizeof(arr[0]);
return 0;
}

Options:​
A) 4​
B) 8​
C) 10​
D) Compilation Error

Q14. What is the output of this Java program?

public class Test {


public static void main(String[] args) {
int arr[] = new int[5];
[Link](arr[2]);
}
}

Options:​
A) 0​
B) Garbage value​
C) Compilation error​
D) Runtime error

Q15. What will be the output of this program?

#include <iostream>
using namespace std;
void modify(int arr[]) {
arr[0] = 10;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
modify(arr);
cout << arr[0];
return 0;
}
Options:​
A) 1​
B) 10​
C) Compilation error​
D) Runtime error

Q16. Identify the error in the following C++ code:


#include <iostream>
using namespace std;
int main() {
int arr[]; // Error
arr[0] = 5;
cout << arr[0];
return 0;
}

Options:​
A) No error​
B) Compilation error​
C) Runtime error​
D) Prints 5

Q17. What is the output of this program?


#include <iostream>
using namespace std;
int main() {
int arr[3] = {5};
cout << arr[1] << " " << arr[2];
return 0;
}

Options:​
A) 5 5​
B) 0 0​
C) Garbage values​
D) Compilation Error
Q18. Identify the error in the following Java code:
public class Test {
public static void main(String[] args) {
int[] arr = new int[-5];
[Link]([Link]);
}
}

Options:​
A) No error​
B) Compilation error​
C) Runtime error​
D) -5

Q19. What will be the output?


public class Test {
public static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5};
for (int i : arr) {
arr[2] = 10;
[Link](i + " ");
}
}
}

Options:​
A) 1 2 3 4 5​
B) 1 2 10 4 5​
C) 1 2 10 10 5​
D) Compilation error

Q20. Identify the error in this Java program:


import [Link].*;
public class Test {
static int[] dp = new int[100];
public static int fib(int n) {
if (n <= 1) return n;
if (dp[n] != 0) return dp[n];
return dp[n] = fib(n - 1) + fib(n - 2);
}
public static void main(String[] args) {
[Link](fib(5));
}
}

Options:​
A) No error​
B) Compilation error​
C) Runtime error​
D) Prints 5

Q21: Pointer Arithmetic Output

#include <stdio.h>

int main() {

int arr[] = {5, 10, 15};

int *p = arr;

printf("%d", *(p + 1));

return 0;

A) 5​
B) 10​
C) 15​
D) Compilation Error

Q22: Character Array vs. String Literal Behavior


#include <stdio.h>

int main() {

char str1[] = "Hello";

char *str2 = "World";

str1[0] = 'h';

str2[0] = 'w';

printf("%s %s", str1, str2);

return 0;

A) hello world​
B) Segmentation Fault​
C) Compilation Error​
D) Undefined Behavior

Q23: Post-Increment in Array Indexing

#include <stdio.h>

int main() {

int arr[] = {10, 20, 30, 40};

int i = 0;
printf("%d %d", arr[i++], arr[i]);

return 0;

A) 10 20​
B) 10 10​
C) Compilation Error​
D) Undefined Behavior

Q24. Find the error in this C++ program:


#include <iostream>
using namespace std;
int main() {
char str[5] = "Hello";
cout << str;
}

Options:​
A) String is too large for char[5]​
B) str should be char str[]​
C) str should be char str[6]​
D) No error

Q25. What will be the output of this code?

#include <iostream>

using namespace std;

int main() {

char str[] = "Hello";

cout << *(str + 1);

}
Options:​
A) H​
B) e​
C) l​
D) o

Q26. What is the issue in this Java program?

class Test {

public static void main(String[] args) {

int arr[] = new int[5];

[Link](arr[5]);

Options:​
A) No error​
B) Array index out of bounds at runtime​
C) [Link] should be inside a loop​
D) arr must be initialized with values

Q27. What is the output of this Bubble Sort implementation?

#include <stdio.h> ​
void bubbleSort(int arr[], int n) { ​
​ for (int i = 0; i < n-1; i++) { ​
​for (int j = 0; j < n-i-1; j++) { ​
​ if (arr[j] > arr[j+1]) { ​
​ int temp = arr[j]; ​
​ arr[j] = arr[j+1]; ​
​ arr[j+1] = temp; ​
​ } ​
​} ​
​ } ​
} ​
int main() { ​
​ int arr[] = {3, 1, 5, 2, 4}; ​
​ bubbleSort(arr, 5); ​
​ for (int i = 0; i < 5; i++) { ​
​printf("%d ", arr[i]); ​
​ } ​
​ return 0; ​
} ​

a) 1 2 3 4 5

b) 5 4 3 2 1

c) 3 1 5 2 4

d) Compilation Error

Q28. What is the output of the following lambda function in C++?

#include <iostream>
using namespace std;
int main(){
auto add = [](int a, int b){ return a+b; };
cout << add(3,4);
return 0;
}
A) 34
B) 7
C) Error
D) Undefined
Q29. Consider an array on which bubble sort is used. The bubble sort
would compare the element
A[x] to which of the following elements in a single iteration
a. A[X]+1
b. A[X]+2
c. A[X+2X]
d. All the above

Q30.
Swathi writes a program to find an element in the array A[5] with the following elements in
order : 8, 30, 40, 45, 70. She runs the program to find a number X. X is found in the first
iteration of binary search. What is the value of X?
a. 40
b. 8
c. 70
d. 30
Answers
(Arrays)
1)​ B
2)​ A
3)​ B
4)​ B
5)​ A
6)​ A
7)​ B
8)​ B
9)​ A
10)​B
11)​B
12)​B
13)​A
14)​A
15)​B
16)​B
17)​B
18)​C
19)​B
20)​D
21)​B
22)​B
23)​A
24)​A
25)​B
26)​B
27)​A
28)​B
29)​A
30)​A

You might also like