IT 401 Assignment 3 Solution

Question One

Do as Directed. (Each question will have 1 mark)

(i) What will be output of the following code?

int i = 0;

while (i != 9)

{

System.out.println(" " + i);

i = i + 2;

}

Answer:

(ii) What will be the output of the following code?

int i = 1;

int sum = 0;

while (i <= 11)

{

sum = sum + i;

i++;

}

System.out.println("The value of sum is " + sum);

System.out.println("The loop will continue " + i + “ times.”);

Answer:

(iii) How many times does the following Java code display "Java Program" output?

for (int i = 0; i < 10; i++);

{

System.out.println(" Java Program ");

}

Answer:

(iv) In the following code, How many times the output “My Name” will be print?

int p;

int q;

for (p = 0; p <= 9; p++)

{

for (q = p; q < 5; q++)

{

System.out.println("My Name");

if (q == 2)

{

q = 6;

}

}

}

Answer:

Question Two

Write a program in Java using “Nested For” Loop to print Diamond star pattern.

*

* *

* * *

* * * *

* * * * *

Provide a screenshot of your work.

Question Three

Write a short notes on Following topics. (each question will have 1 mark)

a. What is an Array?

b. How do declare array in various format?

c. Define Array List.

d. Give an example for add and remove array element.


Question Four

Write a Java Program to display following output using Array List.


Before 2015 Saudi Electronic University Branch: 0

After 2015: 7

Number of Branch: [Dammam, Tabuk, Jeddah, Madinah, Al Qassim, Abha, Al Jouf]

Number of Branch after deletions: 5

Total of Branch: [Dammam, Tabuk, Madinah, Al Qassim, Al Jouf]


Provide a screenshot of your work.