Prepared by Abu Eman 0534532706
Solved Programming 1 Exam
Question 1: Write true "T" or false "F" for each of the following
statements. [7 Marks]
1- Syntax errors or Compile errors are detected by the compiler.
Answer: True (T)
2- Java Micro Edition (Java ME) to develop applications for
client-side applications.
Answer: False (F)
3- Java Runtime Environment (JRE) is a part of the Java
Development Kit (JDK)
Answer: True (T)
4- In Java, each block begins with an opening bracket [ and ends
with a closing bracket ].
Answer: False (F)
5- class is a reserved word or keyword in java.
Answer: True (T)
6- Logic errors are errors that cause a program to terminate
abnormally
Answer: False (F)
7- The application program interface (API) is also known as
library of java.
Answer: True (T)
Question 2: Which of the following java statements is correct or incorrect:
[3 Marks]
1- bool isChecked = "True";
Options: Correct / Incorrect
Answer: Incorrect
2- num *= 3 is not equivalent to num = num * 3
Options: Correct / Incorrect
Answer: Incorrect
3- To use a variable, you must declare it first as: datatype
variableName;
Prepared by Abu Eman 0534532706
Options: Correct / Incorrect
Answer: Correct
4- char initial = 'Mr ';
Options: Correct / Incorrect
Answer: Incorrect
5- This is a valid syntax for declaring a constant: final int
NUMBER = 4;
Options: Correct / Incorrect
Answer: Correct
6- 2print is legal and valid identifier to name the class or
variable.
Options: Correct / Incorrect
Answer: Incorrect
Question 3: Answer these sub-questions: [7 Marks]
3.1) Write Java code that prints "You do a great job" if your
exam mark is 20 AND your project mark is 10. Otherwise, it prints
"You should work hard."
Declare and initialize two integer variables named examMark and
projectMark. Assign 20 to examMark and 10 to projectMark. Use a
logical operator to create a compound condition.
Answer:
int examMark = 20;
int projectMark = 10;
if (examMark == 20 && projectMark == 10) {
[Link]("You do a great job");
} else {
[Link]("You should work hard.");
}
3.2) Match the statements on the right to fit the correct lines
of the code on the left:
public class ExamSwitchCase {
public static void main (String[] args) {
int grade = 50;
//Switch case statement
Line (6)
case 95:
[Link]("A+");
Prepared by Abu Eman 0534532706
Line (9)
case 85:
[Link]("B+");
break;
Line (13)
[Link]("C+");
break;
Line (16)
[Link]("You Failed");
}
}
}
Answer Mapping:
Line (6) -> switch(grade) {
Line (9) -> break;
Line (13) -> case 75:
Line (16) -> default:
Question 4: Answer these sub-questions: [6 Marks]
4.1) Write the output of the following code:
for (int i=1; i<= 10; i++) {
[Link](i + "");
if (i==4) {
break;
}
}
Output: 1234
int sum = 0, x=1;
while (x<5) {
x++;
if (x==2 || x==3) {
continue;
}
sum=sum+x;
}
[Link]("sum is: "+sum);
Output: sum is: 9
int x = -4;
do {
Prepared by Abu Eman 0534532706
x++;
[Link](x + "");
} while (x<0);
Output: -3-2-10
4.2) Write java code that calculates the sum of even numbers from
0 to 20, by using "for loop".
Answer:
int sum = 0;
for (int i = 0; i <= 20; i += 2) {
sum += i;
}
[Link]("The sum of even numbers is: " + sum);
Question 5: Answer these sub-questions: [11 Marks]
5.1) There are 6 errors in the code below, UNDERLINE and CORRECT
the errors:
Original Code:
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
[Link]("Enetr an integer number: ");
Scanner input = new Scanner([Link]);
int number1 = [Link]();
[Link]("Enetr anonther integer number: ");
int number2 = [Link]();
int max [Link](number1, number2);
[Link]("Max number is: "+max);
}
}
Corrections:
1. import [Link]; -> import [Link];
2. Scanner input = new Scanner([Link]); -> Scanner input =
new Scanner([Link]);
Prepared by Abu Eman 0534532706
3. int number1 = [Link](); -> int number1 =
[Link]();
4. int number2 = [Link](); -> int number2 = [Link]();
5. int max [Link](number1, number2); -> int max =
[Link](number1, number2);
6. [Link](...) -> [Link](...);
5.2) Fill in the blanks to get the following output:
Helper Words: System, println, \b, print, \n, \t, toUppercase,
toLowerCase
Answer (Filled Code):
[Link]("Welcome \n ");
[Link]([Link]() + "\n" + lastName);
Question 6: Answer these sub-questions: [6 Marks]
6.1) Answer the following questions:
1. Declare and instantiate on a single line a one-dimensional
array called strArr that holds 50 Strings.
Answer: String[] strArr = new String[50];
2. Declare on one line and instantiate on a second line a one-
dimensional array called number that holds 20 integers.
Answer:
int[] number;
number = new int[20];
6.2) Choose the correct answer for each of the following:
1. The output of the following code is:
public static void main(String[] args) {
int[] intArr = {1,2,3,4,5,6};
for (int i=0; i<[Link]; i+=3){
[Link]("Value: "+intArr[i]);
}
}
Options:
(A) Value: 1\nValue: 4
Prepared by Abu Eman 0534532706
(B) Value: 1\nValue: 2\nValue: 4\nValue: 6
(C) Value: 1\nValue: 3\nValue: 6
(D) Value: 1\nValue: 6
Answer: (A) Value: 1\nValue: 4
2. The output of the following code is:
public static void main(String[] args) {
int[] list={4, 7, 3,9};
int max=list[0];
for (int i = 1; i < [Link]; i++) {
if (list[i]>max)
max=list[i];
}
[Link]("max number is: "+max);
}
Options:
(A) max number is: 9
(B) max is: 9
(C) max number is: 7
(D) max number is: 4
Answer: (A) max number is: 9