Mansoura University Programming Language
Faculty of Engineering Time Allowed: 20 minutes
Computer Eng and Control Sys Dept Total degrees: 15 degrees
November 2025 Midterm Exam
========================================================================
Attempt all given questions and give the correct output Name:
1. int x = 0;
while (x <= 3) { Sec:
[Link](x + " ");
x++; }
A) 0 1 2 B) 1 2 3 C) 0 1 2 3 D) 3 2 1 0
2. for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i+1 ; j++) {
[Link](j + " ");}}
A) 1 2 3 B) 1 2 1 2 3 1 2 3 4 C) 1 2 1 2 3 D) 3 2 1
3. int[] arr = {1, 2, 3, 4};
int sum = 0;
for (int num : ar) {
sum += num;}
[Link](sum);
A) 10 B) 9 C) 4 D) Error
4. int i = 5, j = 10;
while (i < 5 && j > 5) {
i--;
j--;}
[Link]("i: " + i + ", j: " + j);
A) i: 5, j: 10 B) i: 4, j: 6 C) i: 5, j: 6 D) i: 6, j: 5
5. int[] arr = {2, 4, 6, 8, 10};
int result = 0;
for (int i = 0; i < [Link]; i++) {
result += arr[i] * (i % 2 == 0 ? 1 : -1);}
[Link](result);
A) 30 B) -6 C) 10 D) 0
6. import [Link].*;
public class MyClass {
public static void main(String args[]) {
ArrayList<Integer> list = new ArrayList< Integer>();
for (int i = 0; i < 5; i++) {
[Link](i * i); }
[Link](4, [Link](3) + [Link](1));
[Link](list);
A) [0, 1, 4, 9, 10] B) [0, 1, 10, 9, 16]
C) [0, 1, 8, 9, 16] D) [1, 4, 7, 9, 16]
7. import [Link].*;
Stack<Integer> stack = new Stack< Integer>();
for (int i = 1; i <= 5; i++) {
[Link](i); }
while ([Link]() >= 4) {
[Link](); }
[Link](stack);
A) [1, 2] B) [3, 4, 5] C) [1, 2, 3] D) [4, 5]
Page 1 of 3
8. int[] nums = {1, 2, 3, 4, 5};
int i = 0, j = [Link] - 1;
while (i < j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
j--; }
[Link]([Link](nums));
A) [1, 2, 3, 4, 5] B) [5, 4, 3, 2, 1]
C) [1, 5, 2, 4, 3] D) [5, 2, 3, 4, 1]
9. int x = 5, y = 10;
for (int i = 0; i < 3; i++) {
x += i;
y -= i;}
[Link]("x: " + x + ", y: " + y);
A) x: 8, y: 7 B) x: 10, y: 10 C) x: 9, y: 8 D) x: 7, y: 5
[Link] [Link].*;
Hashtable<Integer, String> map = new Hashtable<>();
[Link](1, "A");
[Link](2, "B");
[Link](3, "C");
[Link](4, "D");
for (Integer key : [Link]()) {
if (key % 2 != 0) {
[Link](key, "E");}}
[Link](map);
A) {1=A, 2=E, 3=C, 4=E} B) {1=A, 2=E, 3=E, 4=E}
C) {4=D, 3=E, 2=B, 1=E} D) Error
11. import [Link].*;
Stack<Integer> stack = new Stack<>();
[Link](1);
[Link](2);
[Link](3);
[Link]([Link]() + [Link]() + [Link]());
[Link](stack);
A) [1, 2] B) [1, 5] C) [6] D) [1, 2, 3]
12. int[] arr = {1, 3, 5, 7, 9,12,15};
for (int i = 0; i < [Link]; i++) {
arr[i] = arr[i] % 3 == 0 ? arr[i] / 3 : arr[i] * 2;}
[Link]([Link](arr));
A) [2, 1, 10, 2, 18] B) [2, 3, 10, 7, 18] C) [2, 1, 10, 14, 3, 4, 5] D) [2, 1, 10, 2, 18]
13. public class Test {
public static int recursiveSum(int n) {
if (n <= 0) return 0;
return n + recursiveSum(n - 2); }
public static void main(String[] args) {
[Link](recursiveSum(4)); }}
A) 15 B) 6 C) 12 D) 9
14. public class Test {
public static int mthd(int x, int y) {
if (y == 0) return x;
return mthd(y, x % y);}
public static void main(String[] args) {
Page 2 of 3
[Link](mthd(48, 18)); }}
A) 0 B) 6 C) 18 D) 48
15. public class Test {
public static int recursiveFun(int n) {
if (n <= 1) return n;
return recursiveFun(n - 2) + recursiveFun(n - 3);}
public static void main(String[] args) {
[Link](recursiveFun(5)); }}
A) 8 B) 5 C) 15 D) 0
========================================================
With My Best Wishes
[Link] Sakr
Q Answer Q Answer
1 11
2 12
3 13
4 14
5 15
6
7
8
9
10
Page 3 of 3