0% found this document useful (0 votes)
36 views132 pages

Java 1 Full Revision

Uploaded by

xsaevil
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)
36 views132 pages

Java 1 Full Revision

Uploaded by

xsaevil
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
You are on page 1/ 132

‫‪Total Eight Exams With‬‬

‫‪JAVA Revision‬‬
‫‪344 Questions‬‬

‫تحذير هام‬

‫هذا الملف للمشتركين في موسى اكاديمي فقط‬


‫وغير مسموح بنشره او التعامل به لغير المشتركين‬
‫ونشكركم لحفظكم االمانة وحقوق الملكية‬
‫فريق عمل موسى اكاديمي‬

‫‪Moussa Academy‬‬
‫‪00201007153601‬‬
‫‪WWW.MOUSSAACADEMY.COM‬‬
JAVA Midterm 1
26 Questions

Moussa Academy
00201007153601
WWW.MOUSSAACADEMY.COM
I. Multiple Choice Questions / T & F

1. translates high-level language programs into machine language.


A. Operating System
B. Linked
C. Compiler
D. Debugger
2. The executes the program’s bytecodes, thus performing the actions specified by the program.
A. Interpreter
B. Linker
C. JVM
D. Debugger
3. The file extension used for a compiled Java file is .
A. .class
B. .exe
C. .java
D. .txt
4. The only language a computer can directly understand is that computer’s .
A. Virtual Machine
B. High-level language
C. Statement set
D. Machine language
5. What is process of defining two or more methods within the same class that have the same name but
different parameters declaration.
A. Method overloading
B. Method overriding
C. Method hiding
D. Encapsulation
6. What is the return type of a method that does not return any value?
A. void
B. public
C. string
D. static

1|Page www.MoussaAcademy.com

00201007153601
7. The methods in Java class libraries are known as methods.
A. User-defined
B. Library-defined
C. Pre-defined
D. Parameter
8. A/ An is a collection of classes with a related purpose.
A. Interface
B. Package
C. JVM
D. Container
9. The informal language written in plain English that described the step by step of a problem’s
solution.
A. Program
B. Pseudocode
C. Compiler
D. Instruction
10. Which statement declares a variable that will store a floating-point number ?
A. floating x;
B. double x;
C. 5.1 = x;
D. String x = 5.6;
11. If x and y are int types variables, what will be the result of the expression x/y when x=9 and y=2?
A. 4.5;
B. 4
C. 4.25
D. 4.75
12. The following Java statement has .
System.out.println(“Welcome! To Object oriented exam”)
A. Multiple errors
B. No error
C. A syntax error
D. A run time error
13. What is the output of the following code snippet?
String output = 5 >= 100? “Yes” : “No”;
System.out.print(output);
A. output
B. Yes
C. No
D. 100

2|Page www.MoussaAcademy.com

00201007153601
14. What is the output of the following code snippet?
int number = 2;
switch(number) {
case 1: System.out.print(“First “);
break;
case 2: System.out.print(“Second “);
case 3: System.out.print(“Third “);
break;
default: System.out.print(“Fourth “);
}
A. Second
B. Second Third
C. Second Third Fourth
D. Syntax Error
15. What is the output of the following code snippet?
int x = 3, y = 10;
if (x > 7)
if(x > 7)
if(x > 7)
System.out.println(“y is > 7”);
else
System.out.println(“x <= 7”);
System.out.println(“x is < 7”);
A. y is > 7
B. x is <= 7
C. x is < 7
D. Syntax error
16. What is the output of the following code snippet?
if(10%2 == 0)
System.out.print(“Even “);
else
System.out.print(“Odd “);
System.out.println(“Number”);
A. Even Number
B. Odd Number
C. Number
D. No output because there is no semicolon at the end of the if statement
17. Which of the following class name is correct?
A. Student@
B. Student5
C. Student&
D. 5Student

3|Page www.MoussaAcademy.com

00201007153601
18. A is a location in the computer’s memory where values can be stored for user later in a program.
A. variable
B. class
C. package
D. method
19. A class is used to read an input given by a user.
A. Socket
B. Stream
C. Stack
D. Scanner
20. allows you to trace and remove bugs, errors, and abnormalities from your programs.
A. Editing
B. Troubleshooting
C. Debugging
D. Checking
21. What is the output of the following code snippet?
int counter;
for(counter = 1; counter <= 10; counter++) {
if(counter ==3) {
break;
}
}
System.out.println(“ the counter value is “ + counter);
A. The counter value is 1
B. The counter value is 10
C. The counter value is 3
D. The counter value is 11

4|Page www.MoussaAcademy.com

00201007153601
22. What is the output of the following code snippet?
String gender = "male";
int age = 22;
if(gender == "male" && age > 25) {
System.out.println("First if statement.");
} else if( gender == "female" || age > 25) {
System.out.println("Second if statement.");
} else if ( gender =="male" ^ age == 22) {
System.out.println("Third if statement.");
} else if(gender == "female" || age != 23) {
System.out.println("Fourth statement.");
}
A. First if statement.
B. Second if statement.
C. Third if statement.
D. Fourth if statement.
23. What is the output of the following code snippet?
int n = 1;
do {
System.out.printf("%d ", n);
n+=2;
} while (n <= 5);
System.out.println();
A. 1 2 3 4 5
B. 2 4 5
C. 1 3
D. 1 3 5
24. If a loop exists inside the body of another loop, it’s called a .
A. Static loop
B. nested loop
C. while loop
D. dynamic loop

5|Page www.MoussaAcademy.com

00201007153601
II. Open Questions

25. Write a complete java program that performs the following tasks:
A. Ask the user to enter a positive number of his/her total mark. If the user enters a negative number, then
the program keeps running asking the user to enter the total mark.
B. Once the user enters a positive number, print the student’s grade based on the obtained mark as the
following
a. If the student’s mark is greater than or equal to 60, print “Passed”.
b. If it is not, print “Failed”.
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter the total mark (positive number): ");
int totalMark = scanner.nextInt();
if (totalMark > 0) {
if (totalMark >= 60) {
System.out.println("Passed");
} else {
System.out.println("Failed");
}
break; // Exit the loop after a valid input
}
}
scanner.close();
}
}

6|Page www.MoussaAcademy.com

00201007153601
26. Find the output of the following Java program:
public class main {
public static void main( string[] args)
{
System.out.println(“hello!”);
int a = 3, b = 1, c = 2;
a = ++b;
int max = Math.max(c,3);
System.out.println(“a: “, a);
System.out.println(“b: “, b);
System.out.println(“max: “, max);
int result = getResult(max);
System.out.println(“result: “, result);
}
public static int getResult(int m) {
return m * m *m;
}
}

hello!
a: 2
b: 2
max: 3
result: 27

7|Page www.MoussaAcademy.com

00201007153601
JAVA Midterm 2
27 Questions

Moussa Academy
00201007153601
WWW.MOUSSAACADEMY.COM
I. Multiple Choice Questions

1. Which of the following components transforms bytecode into machine specific code?
a) JKI
b) JVM
c) JRE
d) JTI

2. To display multiple lines of text with a single statement, the special escape character
a) \n
b) \r
c) \**
d) \\

3. What is the output of the following snippet code?

if (1>3)
System.out.println("Hello");
System.out.println("World");

a) World
b) Hello
World
c) Hello
d) There is no output due to compilation errors

4. What is the output of the following snippet code?


int x = 10, y =2;
System.out.println(x*y + "-" + x%y);

a) 10*2-0
b) 20-1
c) 10*2-1
d) 20-0

5. ___________is used to exit from a given loop statement.


a) Block
b) Break
c) Pass
d) Continue

1|Page www.MoussaAcademy.com

00201007153601
6. To embed program documentation directly into java programs we use____________
a) //
b) ./
c) /** and */
d) %* and*%

7. You would use ___________ to repeat a block of codes for several known times.
a) if statements
b) for loops
c) java classes
d) switch

8. What is the output of the following snippet code?


String s = 5<4 ? "TRUE" : "FALSE";
System.out.println(s);

a) TRUE: FALSE
b) Run time error
c) FALSE
d) TRUE

9. The following code will print Welcome to OOP __________ times.


int counter = 1;
while (counter <= 5) {
System.out.println("Welcome to OOP");
counter++;

a) 5
b) 4
c) 0
d) 6

2|Page www.MoussaAcademy.com

00201007153601
10. What is the output of the following code snippet if the input is 25?
int i;
Scanner in = new Scanner(System.in);
System.out.print("Enter number ? ");
i = in.nextInt();
if(i > 25){
i++;
} else{
i--;
}
System.out.print(i);

a) 26
b) 24
c) 25
d) 27

11. The main functionality of _____________ is to convert high-level language programs into machine
language.
a) Memory handlers
b) Assembly language
c) Compiler
d) Machine language

12. What is the output of the following snippet code?


int x = 50;
int y = 30;
System.out.println(x>30 && y>40);

a) False
b) The code has run time error
c) True
d) The code has a syntax error

3|Page www.MoussaAcademy.com

00201007153601
13. What is the output of the following code snippet?
double income = 6000;
double cutoff = 5000;
double minIncome = 3000;
if(minIncome > income){
System.out.println("Minimum income requirement is not met.");
}
if(cutoff < income){
System.out.println("Maximum income limit is exceeded.");
}
else {
System.out.println("Income requirement is met.");
}

a) There’s no output
b) Maximum income limit is exceeded
c) Income requirement is met
d) Minimum income requirement is not met

14. Which of the following is an example of relational operators?


a) -
b) +
c) %
d) <

15. Which of the following statement declares a variable that will store an integer value?
a) String count = 5;
b) count = 5;
c) integer count
d) int count;

16. ___________ is a procedure for solving a problem in terms of the actions to execute and the order is
which they execute.
a) Firmware program
b) Hardware program
c) Algorithm
d) Software bug

4|Page www.MoussaAcademy.com

00201007153601
17. What is the output of the following java code?
public class A {
public static void main(String[] args) {
int I = 24;
String S = "Hello";
System.out.println("%d %s", I, S);
}
}

a) 24 Hello
b) 23
c) Hello
d) I S

18. When a Java program is compiled, the compiler creates a file that has the file extension.
a) .txt
b) .js
c) .java
d) .class

19. Any computer would understand only its own machine language, which is known as machine _______
a) dependent
b) independent
c) shared
d) isolated

5|Page www.MoussaAcademy.com

00201007153601
20. What is the output of the following java code:
public class A {
public static void main(String[] args) {
char grade = 'L';
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
case 'C':
System.out.println("Well done");
break;
case 'D':
System.out.println("You passed");
break;
case 'F':
System.out.println("Better try again");
break;
default:
System.out.println("Invalid grade");

}
}
}

a) Well done
b) Great
c) Invalid grade
d) Excellent!

21. What is the output of the following java program?


public class Test {
public static void main(String[] args) {
int x = 10;
x = 20;
System.out.println(x);
}
}

a) 10
b) 20
c) no output
d) compile-time error

6|Page www.MoussaAcademy.com

00201007153601
22. In java each statement ends with
a) :
b) ;
c) )
d) }

23. A/an ____________ would be used to have a loop that is inside another loop.
a) nested if statement
b) nested loop
c) if statement
d) method

24. What is the output of the following java code?


public class A {
public static void main(String[] args) {
if(1+1+1+1+1 == 5){
System.out.print("TRUE ");
}
else{
System.out.print("FALSE ");
}
System.out.print("Statement");
}
}

a) True
b) FALSE Statement
c) False
d) TRUE Statement

25. In java, which of the following is a valid Identifier?


a) Test_Program
b) 9TestProgram
c) Test Program
d) Test’program

7|Page www.MoussaAcademy.com

00201007153601
II. Essay Questions

1. Write a complete java program that does the following:

a. Asks the user to give his/her name and store it in a variable name.

b. Asks the user to give a number and store it in a variable number.

c. Find and print the even numbers from 2 to number using for loop.

 Examples of the output:


Enter your Name?
Sara Algamdi
Enter a number
6
246

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Ask for user's name


System.out.print("Enter your name? ");
String name = scanner.nextLine();

// Ask for a number


System.out.print("Enter a number: ");
int number = scanner.nextInt();

// Find and print even numbers from 2 to number


for (int i = 2; i <= number; i += 2) {
System.out.print(i + " ");
}
}
}

8|Page www.MoussaAcademy.com

00201007153601
2. Find the output of the following Java Program?
public class TestMidTerm {
public static void main(String[] args) {
System.out.println("Hello");
int sum = 0;
for (int i = 1; i < 2; i++) {
sum = sum + i;
for (int z = 0; z <= 1; z++) {
System.out.println("i= " + i + " .. z= " + z);
}
}
System.out.println("sum = " + sum);
System.out.println("End!");
}
}

Hello

i= 1 .. z= 0

i= 1 .. z= 1

sum = 1

End!

9|Page www.MoussaAcademy.com

00201007153601
JAVA Midterm 3
60 Questions

Moussa Academy
00201007153601
WWW.MOUSSAACADEMY.COM
I. Questions and Answers

Answer: D
Explanation: A: nextInt(), B: nextDouble(), C: nextLine() but next() takes first token only (stops until it first
space).

Answer: C
Explanation: A,B,C are not key words of java

1|Page www.MoussaAcademy.com

00201007153601
Answer: A
Explanation: x < 5 || x > 10 means x < 5 or x > 10, so anything less than 5 is TRUE, 4 < 5 YES.
and OR (||) only requires one of the two statements to be true in order to be true.

Answer: A
Explanation: in switch-case with break statement is like if-condition, only ONE condition (case) is executed
when break statement is used, here, the char ‘x’ has value ‘+’, so the first case is executed, n1+n2 = 10 + 15 = 25

2|Page www.MoussaAcademy.com

00201007153601
Answer: D
Explanation: A: type of java edition, B: JRE is Java Runtime Environment, the development libraries and
classes C: JVM is the Java Virtual Machine, where java bytecodes are run

Answer: D. Fixed (or known)

3|Page www.MoussaAcademy.com

00201007153601
Answer: D
Explanation:
Semantic error: errors due to meaning/context
Math error: errors due to math operations like division by zero
Logic error: errors due to incorrect program flow/number results (like incorrrect sum)
Syntax error: errors due to wrong rules in typing of language code (like semicolon;)
= is used for assignment
== is used for comparison

Answer: C
Explanation:
break: exit loop forcefully
return: end of function
exit(): function to quit current executing program

4|Page www.MoussaAcademy.com

00201007153601
Answer: C
Explanation: 2/10 is 0 (integer division) which is greater than 0

Answer: D
Explanation: A and C are error (int and double)
B will take only first name, we need to take the full name (all of the line)

5|Page www.MoussaAcademy.com

00201007153601
Answer: D

Answer: A

6|Page www.MoussaAcademy.com

00201007153601
Answer: B
Explanation: The loop will run as follows:
at i = 0, check 0==4, NO  print 0
at i = 1, check 1==4, NO  print 1
at i = 2, check 2==4, NO  print 2
at i = 3, check 3==4, NO  print 3
at i = 4, check 4==4, YES  BREAK (leave loop entirely)

Answer: C
Explanation: Selection statement are statements that select a certain execution/program flow/code block
depending on a condition, so we choose IF-ELSE or SWITCH-CASE

7|Page www.MoussaAcademy.com

00201007153601
Answer: B

Answer: A

Answer: B
Explanation: \” escape character for “ and \n prints new line

8|Page www.MoussaAcademy.com

00201007153601
Answer: D

Answer: D
Explanation: number % 2 returns remainder of division by 2, so if remainder is zero (==0), then this number
must be even, it is a very common code snippet used, hence, we choose D

9|Page www.MoussaAcademy.com

00201007153601
Answer: C
Explanation:

10 | P a g e www.MoussaAcademy.com

00201007153601
Answer: A

Answer: D
Explanation:
[ ] Printed normally
%60 means reserve 60 spaces
S means print the string to come in UPPER CASE

Answer: D

11 | P a g e www.MoussaAcademy.com

00201007153601
Answer: C

Answer: A

12 | P a g e www.MoussaAcademy.com

00201007153601
Answer: C

Answer: B
Explanation: It has the ordering wrong, it should bne cost = 72 not the other way around. Also, the
variable name is incorrect to begin with, variables cannot start with digits

Ansswer: B

13 | P a g e www.MoussaAcademy.com

00201007153601
Answer: B

Answer: C

Answer: B
Explanation: A: cannot start with number
C: Cannot have special symbol *
D_:cannot have spaces

14 | P a g e www.MoussaAcademy.com

00201007153601
Answer: C

Answer: C

Answer: A

15 | P a g e www.MoussaAcademy.com

00201007153601
Explanation:
%s first string: “1”
! printed normally
%s for second string: “234”
%n for new line (like \n)
%s for third string “6767”
%n for new line (not visible because end of code)

Answer: A
Explanation: Runtime errors are errors that stop the program only during its running (runtime) and does not
prevent compilation.

Answer: D

Answer: A

16 | P a g e www.MoussaAcademy.com

00201007153601
Answer: C

Answer: C

17 | P a g e www.MoussaAcademy.com

00201007153601
False

True
True

False
C

A
B

D
T

T
B

B
A

F
B

D
T

F
F
JAVA Midterm 4
29 Questions

Moussa Academy
00201007153601
WWW.MOUSSAACADEMY.COM
I. Multiple Choice Questions

1. Which package in Java provides classes and Interfaces for data structures like lists, sets, and maps?
a) java.net
b) java.io
c) java.util
d) java.lang

2. Which of the following is an example of method overloading for:


public int add(int a, int b)

a) public void add(int[] numbers)


b) public int add(int a, int b. int c)
c) public int addition(int a, int b)
d) public double add(int a, int b)

3. Operator (?:) can replace which of the following statements?


a) If...else statement
b) Break statement
c) If statement
d) Switch statement

4. The method name should be a ___________ and start with a ___________ letter.
a) Verb / Uppercase
b) Noun / Lowercase
c) Adjective / Uppercase
d) Verb / Lowercase

5. ____________ In Java makes computer execute statements one after another.


a) Main method
b) Program structure
c) Sequence structure
d) Data type

1|Page www.MoussaAcademy.com
6. Which of the following statements is true about Java API packages?
a) Java API packages provide a collection of classes and interfaces for various programming tasks.
b) Java API packages can't be used for creating custom applications.
c) Java API packages are only used for internal purposes and cannot be accessed by developers.
d) Java API packages are specific to a particular IDE and cannot be used in other Java environments

7. What is the scope of a local variable in Java?


a) The scope of a local variable is limited to the entire application.
b) The scope of a local variable is limited to the block or method in which it is declared.
c) The scope of a local variable is limited to the class in which it is declared.
d) The scope of a local variable is limited to the package in which it is declared.

8. What is the output of the following Java program?


public class Test {
public static void findMinimum(int x, int y) {
if (x < y)
System.out.println("x is the smallest");
else
System.out.println("y is the smallest");
}

public static void main(String args[]) {


findMinimum(60, 65);
}
}

a) y is the smallest
b) 65
c) 60
d) x is the smallest

9. Which of the following statements is true about method overloading in Java?


a) Method overloading allows multiple methods with the same name and return type.
b) Method overloading allows multiple methods with the same name but different parameters.
c) Method overloading allows multiple methods with the same name but different access modifiers.
d) Method overloading allows multiple methods with the same name but different exception types.

2|Page www.MoussaAcademy.com
10. Which of the following is not a valid example of method overloading for display method?
a) public int display(int num)
b) public void display(int num1, int num2)
c) public void display(String str)
d) public void display(int num)

11. Which package in Java provides classes and Interfaces for basic input/output operations?
a) java.util
b) java.lang
c) java.net
d) java.io

12. One of the following is Invalid Identifier (e.g. user defined variable) in Java programming language.
a) Welcome1
b) $welcome_
c) 1welcome_
d) _welcome1

13. The following method declaration return a/an ________________ value/s.


public String print(int x, String y) {
//code here
}

a) Integer and String


b) String
c) Integer
d) String and double

14. Which statement is a selection statement?


a) If statement
b) continue statement.
c) for() loop
d) break Statement

3|Page www.MoussaAcademy.com
15. sqrt() is a ____________ method of Math class. It returns the square root of a number.
a) Predefined
b) Modified
c) User-defined
d) Customized

16. What is the return type of a method that does not return any value?
a) double
b) void
c) float d
d) int

17. To calculate the maximum number of two variables x and y, we can use the following java statement:
a) Math.maximum(x,y);
b) Math.max(x,y);
c) x.Math maximum(y)
d) y.Math.max(x)

18. The procedure of solving a problem in regards to action and sequence order called_______________.
a) Java Code
b) Algorithm
c) Program
d) Pseudocode

19. In Java, an expression of a switch statement must be one of the following types.
a) Byte, short, int, long, enums and string
b) int, float, double
c) int, short, byte, double
d) String, float, double

20. The Informal language that cannot be executed on a computer is ____________.


a) High-level languages
b) Assembly language
c) Pseudocode
d) Low-level language

4|Page www.MoussaAcademy.com
21. The Escape Sequence ____________ is used to print double-quote (") inside print statement.
a) q\
b) \”
c) \Esc
d) \q

22. A/An ______________ Is a location in the computer's memory where a value can be stored for later
uses in a program
a) Input
b) Method
c) Scanner
d) Variable

23. To display formatted text, ______________ can be used.


a) System.out.printd (format, arguments);
b) System.out.printf(format, arguments);
c) System.out.print(arguments);
d) System.out.println(arguments);

24. In Java, _______________ executes a block of code if a condition is true, but if false it executes another
block of code.
a) If statement
b) Continue statement
c) If... else statement
d) Switch statement

25. The input to a method is called a(n) ____________ .


a) Parameter
b) Procedure
c) Overloaded
d) Interface

26. To read Input from the keyboard (e.g. from the user), we have to create and use a/an _______ object.
a) Integer
b) Variable
c) Input
d) Scanner

5|Page www.MoussaAcademy.com
27. The ____________ Is the routine process of locating and removing bugs, errors or abnormalities from
programs.
a) Commenting
b) Debugging
c) Compiling
d) Declaration

II. Coding Questions

28. Using for loop write Java code that display the following:
Output example:
SEU-1
SEU-2
SEU-3

public class DisplayOutput {


public static void main(String[] args) {
// We use a "for" loop to iterate from 1 to 3.
for (int i = 1; i <= 3; i++) {
// After each iteration, i is incremented by 1 (i++).
// System.out.println() to display a line of text.
// The line contains "SEU-" and the value of i.
System.out.println("SEU-" + i);
// The loop continues until i becomes greater than 3.
}
}
}

6|Page www.MoussaAcademy.com
29. Solve the following.
a. Write a complete program to calculate and print the value of the following
algebraic expressions:

public class AlgebraicExpressions {


public static void main(String[] args) {
// Calculate the values of the expressions as integers
int x = (55 + 12 + 77 - 12) / 22;
int y = 33 * 22 / 3 + 12;

// Because Math.pow only accepts double values we set z to double


double z = Math.pow(4, 2) + (Math.pow(6, 2)/3);

// Print the results


System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);
}
}

b. What is the correct value of x, y, and z?

x=6

y = 254

z = 28.0

7|Page www.MoussaAcademy.com
JAVA Final 2021
29 Questions

Moussa Academy
00201007153601
WWW.MOUSSAACADEMY.COM
IT232 Final Fall 2021

Moussa Academy
00201007153601
WWW.MOUSSAACADEMY.COM
1|Page www.MoussaAcademy.com

00201007153601
College of Computing and Informatics
RM

Final Examina on (Marking Criteria)


First Semester: 1442 / 1443 - 2021 / 2022

Paper Information
Object Oriented
Course Title: Programming Course Code: IT232
Number of Pages:
(including cover
Exam Duration: 120 Minutes page) 16

Exam Day/Date: 23/12/2021 Exam Time: 07:00 – 09:00 pm

Student Details
Student
Student Name: ID:
Instructor
CRN: :

Branch:

Exam Instructions
• Fill the above ‘Student Details’ section carefully and complete.
• You are advised to count the number of pages and compare to the number written
above.
• Electronic devices are NOT permitted. Turn off your electronic devices and put
them in the plastic bag under your seat.
• Put your ID card on your desk with your photo face up.
• Use Blue-color pen only.
• You are not allowed to leave the examination room during the first 30 minutes of
the start of Examination.
• Cheating is strictly prohibited and action will be taken.

Marking Scheme Learning Outcomes Achievement


Question Actual Score / Learning Question
Student’s Achievement
No. Max. Score Outcome #

1
ti
College of Computing and Informatics
RM

Q1.1
Q1 /25 LO1 ⬜ Yes ⬜ No
Q1.13
Q2 /10 LO2 ⬜ Yes ⬜ No
Q1.8
Q3 /15 LO3 ⬜ Yes ⬜ No
Final Q1.23
Score:
/ 50 LO4 ⬜ Yes ⬜ No

Q1.2
LO5 ⬜ Yes ⬜ No

1) Circle ONE correct answer for each of the following MCQs in the table below.

Actual
Score
1 2 3 4 5 6 7 8 9 10
25
a a a a a a a a a a
b b b b b b b b b b
c c c c c c c c c c
d d d d d d d d d d

11 12 13 14 15 16 17 18 19 20
a a a a a a a a a a
b b b b b b b b b b
c c c c c c c c c c
d d d d d d d d d d

21 22 23 24 25
a a a a a
b b b b b

IT232 2
College of Computing and Informatics
RM

c c c c c
d d d d d

1. Which one of the following statements is a valid initialization of an array named


shapes of four elements? (LO1) (Nahlah Algethami) [6]
a. String [] shapes = {“Circle”,” Rectangle”,”Square”);
b. String shapes [3] = {“Circle”,” Rectangle”,”Square”);
c. String [] shapes =[“Circle”,” Rectangle”,”Square”];
d. String [3] shapes ={“Circle”,” Rectangle”,”Square”);

2. Complete the following code snippet with the correct enhanced for loop so it iterates
over the array without using an index variable. (LO5) (Nahlah Algethami) [6].
int [] evenNo = {2,4,6,8,10};
___________________
System.out.print(e+" ");

a. for(e: int evenNo)


b. for(evenNo []: e)
c. for(int e: evenNo)
d. for(int e: evenNo[])

3. What is the output of the following Java code? (LO4,LO5) (Nahlah Algethami) [6].
int []myArray = new int [5];
for(int i=0; i< myArray.length; i++)
{
if(i%2 ==0)
myArray[i] = i+1;
System.out.print(" "+myArray[i]);
}

a. 1 0 3 0 5
b. 1 2 3 4 5
c. 0 0 0 0 0
d. 1 0 0 0 0

4. If class Student is subclassed from class Person then which is the correct syntax? (LO5)

IT232 3
College of Computing and Informatics
RM

(Nahlah Algethami) [Week 12].


a. class Student:Person{}
b. class Student extends Person{}
c. Class Student extends class Person{}
d. class Student implements Person{}

5. Find Superclass and Subclass in the below Java code snippet? (LO5) (Nahlah Algethami)
[Week 12].
class B
{
void show(){}
}
class A
{
void hide(){}
}
a. B is superclass and A is subclass
b. A is superclass and B is a subclass
c. There is no superclass or subclass present
d. A is subclass and there is no superclass

6. When we compile Hello.java file, the _____ produces a _________ file. (LO1) (Nahlah
Algethami) [chapter 1].
a. Compiler, Hello.class
b. Interpreter, Hello.java
c. Debugger, Hello.exe
d. Class, Hello.class

7. Which polymorphism behavior do you see in below class? (LO1) (Nahlah Algethami)
[Week 12].
class Draw{
public void Color(int x) { }
public void Color(int x, int y) { }
public void Color(int x, int y, int z) { }
}
a. Method overloading

IT232 4
College of Computing and Informatics
RM

b. Constructor overloading
c. Method overriding
d. Upcasting

8. What is the output of the following Java code? (LO3, LO4, LO5) (Nahlah Algethami)
[chapter 4].

int i = 0;
for(i = 0; i < 10; i++){
break;
}
System.out.println(i);

a. 1
b. 0
c. 10
d. 9

9. What is the output of the following Java code? (LO4, LO5) (Nahlah Algethami) [chapter
5].
public class Test{
public static int sum(int a){
return a +5;}
public static int sum(int a, int b){
return a+10;
}

public static void main(String []args){


System.out.print(" " +sum(1));
System.out.print(" " +sum(5));
} }

a. 6 10
b. 6 15
c. 11 15
d. 1 5

IT232 5

College of Computing and Informatics


RM

10. What will be the value inside the variables x and y after the given set of assignments?
(LO1) (Dr Mohd Uruj Jaleel) [Chapter Number 2 ] Week 4
int x = 20;
int y = 10;
x = (x - y) * 2;
y = x / 2;

a. x = 40, y = 20
b. x = 20, y = 10
c. x = 10, y = 20
d. x = 20, y = 20

11. What is the printed value of x? (LO1) (Dr Mohd Uruj Jaleel) [Chapter Number 4]
Week 6
int x = 6;
for(int i = 5 ; i>1; i --)
x --;
System.out.println(x);

a. 2
b. 1
c. 0
d. No value of x will be printed because the loop will never stop

12. Which of these keywords is used to prevent content of a variable from being
modified? (LO1) (Dr Mohd Uruj Jaleel) [Chapter Number 8 ] Week 11

a. final
b. last
c. constant
d. static

13. Which of the following is the correct way of importing an entire package ‘pack’?
(LO2) (Dr Mohd Uruj Jaleel) [Chapter Number 8] Week 11

IT232 6
College of Computing and Informatics
RM

a. import pack.;
b. Import pack.;
c. import pack.*;
d. Import pack.*;

14. What will be the output of the following Java code? (LO1) (Dr Mohd Uruj Jaleel)
[Chapter Number 5 ] Week 7
class overload
{
int x;
int y;
void add(int a)
{
x = a + 1;
}
void add(int a, int b)
{
x = a + 2;
}
}
class Overload_methods
{
public static void main(String args[])
{
overload obj = new overload();
int a = 0;
obj.add(6);
System.out.println(obj.x);
}
}
a. 5
b. 6
c. 7

IT232 7
College of Computing and Informatics
RM

d. 8

15. A ____________ is needed to terminate recursion. (LO1) (Dr Mohd Uruj Jaleel)
[Chapter Number 18] Week 14

a. recursion step
b. break statement
c. void return type
d. base case

16. Which component is responsible for conver ng bytecode into machine speci c code?
(LO1) (Afra Alrubaiaan) [Chapter 2]

a. JVM
b. JDK
c. JIT
d. JRE

17. Find the output of the given Java program? (LO1) (Afra Alrubaiaan) [Chapter 3]
public class Main
{
public static void main(String[] args) {
boolean x, y, z;
x = y = z = true;

if(!x || (!y && !z))


System.out.print("Hi");
else
System.out.println("Hello");
}
}
a. Hi
b. Hello
c. Compiled Successfully, No Output
d. HiHello

IT232 8
ti
fi
College of Computing and Informatics
RM

18. Each class you create becomes a new ________ that can be used to declare variables
and create objects. (LO2) (Afra Alrubaiaan) [Chapter 7]

a. package
b. instance
c. library
d. type

19. ___________________ is g keywords is used to implement an interface in java?


(LO1) (Nahlah Algethami) [Week 12].
a. implements
b. extends
c. super
d. final
20. If a class does not de ne constructors, the compiler provides a default constructor
with no parameters, and the class's instance variables are ini alized to ________.
(LO2) (Afra Alrubaiaan) [Chapter 7]

a. zero
b. null
c. their default values
d. false

21. Java requires a ________ call for every object that's created. (LO2) (Afra Alrubaiaan)
[Chapter 7]
a. Constructor
b. destructor
c. parameterless

IT232 9
fi
ti
College of Computing and Informatics
RM

d. parameterized

22. Indicate the result of comparing the rst string: "Apples", with the second string:
"Oranges". (LO1) (Afra Alrubaiaan) [Chapter 14]
a. less than
b. equal
c. greater than
d. true

23. Which of these methods of String class is used to obtain character at speci ed index?
(LO4) (Afra Alrubaiaan) [Chapter 14]
a. char()
b. Charat()
c. charat()
d. charAt()

24. Which of these methods of String class can be used to test two strings for equality?
(LO4) (Afra Alrubaiaan) [Chapter 14]
a. isequal()
b. isequals()
c. equal()
d. equals()

IT232 10
fi
fi
College of Computing and Informatics
RM

25. What will be the output of the following Java program? (LO5) (Afra Alrubaiaan)
[Chapter 14]
class string_class
{
public static void main(String args[])
{
String obj = "hello";
String obj1 = "world";
String obj2 = obj;
obj2 = " world";
System.out.println(obj + " " + obj2);
}
}
a. hello hello
b. world world
c. hello world
d. world hello

[25 Marks]

IT232 11
College of Computing and Informatics
RM

2) For each ques on, circle True (T) or False (F) as appropriate.
Actual
Score

10

1. An array can store many different types of values. T F


(LO1) (Nahlah Algethami) [6]
2. To get the number of elements in an Array, we write T F
MyArray.length()
(LO1) (Nahlah Algethami) [6]
3. In java, all arguments are passed by value. (LO1) (Nahlah Algethami) T F
[6]
4. A has-a relation ship is implemented via inheritance. (LO1) (Nahlah T F
Algethami) [ week 12]
5. Java support multiple inheritance of classes. (LO1) (Nahlah T F
Algethami) [ week 12]
6. Final class cannot be inherited. (LO1) (Nahlah Algethami) [ week 12] T F
7. When a recursive method is called to solve a problem, it is actually T F
capable of solving only the simplest case(s), or base case(s). (LO1)
(Dr Mohd Uruj Jaleel) [Chapter Number 18] Week 14
8. A static variable represents class-wide information that’s shared by T F
all the objects of the class. (LO2) (Dr Mohd Uruj Jaleel) [Chapter
Number 8] Week 11
9. Recursion can be efficient in computation because of reduced T F
memory-space usage. (LO1) (Dr Mohd Uruj Jaleel) [Chapter Number
18 ] Week 14
10. The following is a correct declaration of an enum: T F
enum Level {
LOW,
MEDIUM,
HIGH
}
(LO1) (Dr Mohd Uruj Jaleel) (Dr. Abdelwaheb) [Chapter Number 8]
Week 11
11. Keyword final specifies that a variable is not modifiable after T F
initialiation. (LO2) (Dr Mohd Uruj Jaleel) [Chapter Number 8] Week
11

IT232 12
ti
College of Computing and Informatics
RM

12. Variables or methods declared with access modi er private are T F


accessible only to methods of the class in which they're declared.
(LO2) (Afra Alrubaiaan) [Chapter 7]
13. Constructors have explicit return type. (LO2) (Afra Alrubaiaan) T F
[Chapter 7]
14. Object is an en ty that you can manipulate in your programs by T F
calling methods. (LO2) (Afra Alrubaiaan) [Chapter 7]
15. The == operator can be used to compare two String objects. The T F
result is always true if the strings are iden cal. (LO1) (Afra
Alrubaiaan) [Chapter 14]
16. A class is sequence of instruc ons that accesses the data of an T F
object. (LO2) (Afra Alrubaiaan) [Chapter 7]
17. The "String" class is an immutable class. (LO1) (Afra Alrubaiaan) T F
[Chapter 14]
18. A String can be modi ed a er it’s created. (LO1) (Afra Alrubaiaan) T F
[Chapter 14]
19. System.out.println(Pa ern.matches(".s", "books")); this statement T F
returns false (because it has more than 2 char). (LO5) (Afra
Alrubaiaan) [Chapter 14]
20. System.out.println(Pa ern.matches("[a-zA-Z0-9]{6}","EX#123")); T F
this statement returns true. (LO5) (Afra Alrubaiaan) [Chapter 14]

[10 Marks]

IT232 13
ti
tt
tt
fi
ft
ti
ti
fi
College of Computing and Informatics
RM

3) Open Ques ons.


Actual
Score

15
a) What is the output of the following Java code?
(LO4,LO5) (Nahlah Algethami) [Week 12].

class Vehicle{

protected String brand ="Kia";


protected String color = "White";
protected int year = 2020;
void run()
{
System.out.println(" Vehicle is moving");
}
}

class Car extends Vehicle{

void display(){
System.out.println(" brand: "+brand +" color:
"+color + " year: "+year);}

void stop(){
System.out.println(" Car stopped moving");}
}

public class TestFinal{


public static void main(String args[]){

Car c = new Car();


c.display();
c.run();

IT232 14
ti
College of Computing and Informatics
RM

c.stop();
}
}

Answer for the Marking Criteria


brand: Kia color: White year: 2020 [1 Mark]
Vehicle is movin [1 Mark]
Car stopped movin [1 Mark]

[3 Marks]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

b) Write a complete java program that perform the following tasks: (LO 4) [Nahlah
Algethami] [6]


Declare and ini alize an array of int numbers with the following elements
10,11,12,13,14,15
• Calculate and print the sum of odd and even numbers of the array.
Answer for the Marking Criteria

public class Final {


public static void main(String[] args) {

int[] myArray = {10,11,12,13,14,15} [0.5 Mark]


int sum_even =0;
int sum_odd =0 ;

for (int i = 0; i < myArray.length; i++ [0.5 Mark]


{
if(i%2==0 [0.5 Mark
sum_even+=myArray[i] [0.5 Mark]
else
sum_odd+=myArray[i] [0.5 Mark]
}

System.out.println("sum of even numbers is "+sum_even); [0.25 Mark]


System.out.println("sum of odd numbers is "+sum_odd); [0.25 Mark]

[3 Marks]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
c) Write a complete java program that perform the following tasks: (LO5) (Afra

IT232 15
)
ti
g
g
;
;
;
)



]
College of Computing and Informatics
RM

Alrubaiaan) [Chapter 14]


Asks the user to input two strings.

Uses the method compareTo of the class String to compare the two strings
input by the user. Output whether the rst string is less than, equal to or
greater than the second.
Answer for the Marking Criteria

import java.u l.Scanner;


class CompareStrings
{ [0.5 Mark]
public sta c void main(String args[])
{
String s1, s2;
Scanner in = new Scanner(System.in);

System.out.println("Enter the rst string"); [0.5 Mark]


s1 = in.nextLine();

System.out.println("Enter the second string"); [0.5 Mark]


s2 = in.nextLine();

if (s1.compareTo(s2) > 0) [0.5 Mark]


System.out.println("The rst string is greater than the second.");
else if (s1.compareTo(s2) < 0) [0.5 Mark]
System.out.println("The rst string is smaller than the second.");
else [0.5 Mark]
System.out.println("Both the strings are equal.");
}
}

[3 Marks]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d) Find the output of the following code: (LO5) (Dr Mohd Uruj Jaleel) [Chapter Number
8] Week 11

class Demo{
int value1;
int value2;
Demo(){
value1 = 1;
value2 = 2;
System.out.println("Inside 1st Constructor");
}
Demo(int a){
value1 = a;
System.out.println("Inside 2nd Constructor");
}
Demo(int a,int b){

IT232 16
ti
ti
fi
fi
fi
fi
College of Computing and Informatics
RM

value1 = a;
value2 = b;
System.out.println("Inside 3rd Constructor");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
}
public static void main(String args[]){
Demo d1 = new Demo();
Demo d2 = new Demo(3);
Demo d3 = new Demo(3,4);
d1.display();
d2.display();
d3.display();
}
}
Answer for the Marking Criteria
Inside 1st Constructo [0.5 Mark]
Inside 2nd Constructo [0.5 Mark]
Inside 3rd Constructo [0.5 Mark]
Value1 === [0.25 Mark]
Value2 === [0.25 Mark
Value1 === [0.25 Mark]
Value2 === [0.25 Mark]
Value1 === [0.25 Mark]
Value2 === [0.25 Mark
[3 Marks]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
e) Write a complete java program that perform the following tasks: (LO4) (Dr Mohd Uruj
Jaleel) [Chapter Number 18] Week14

• Asks the user to input a posi ve number. If the user enters a nega ve
number, then the program keeps asking for a posi ve one.
• Find out and print the factorial of the given number.
Note: factorial of 5 is equal to: 5*4*3*2*1 = 120

Answer for the Marking Criteria


First method
import java.util.Scanner;
class FactorialDemo{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in); [0.5 mark]
int num;
do
{
System.out.println("Enter a positive number:");
num = scanner.nextInt(); [1 Mark]
}while(num<0);

IT232 17
2
1
4
3
0
3
ti
r
r
r
ti
]
]

ti
College of Computing and Informatics
RM

int f = fact(num);
System.out.println("Factorial of entered number is: " + f); [0.5 mark]

static int fact(int n)


{
int output;
if(n==1){
return 1; [1 Mark]
}
output = fact(n-1)* n;
return output;
}
}

second method

import java.util.Scanner;
class FactorialDemo{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in); [0.5 mark]
int num;
do
{
System.out.println("Enter a positive number:");
num = scanner.nextInt(); [1 Mark]
}while(num<0);

f =1;
for(int i =1;i<=num;i++)
f = f * i; [1.5 Mark]
System.out.println("Factorial of entered number is: " + f);
}

[3 Marks]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

IT232 18
College of Computing and Informatics
RM

End of Examination… Good Luck!

IT232 19
JAVA Final 2022
73 Questions

Moussa Academy
00201007153601
WWW.MOUSSAACADEMY.COM
Revision 73 CS140-IT232-
Questions & Answers
DS140-CS230-
IT401

Moussa Academy
00201007153601
WWW.MOUSSAACADEMY.COM
I. Questions

1. If a class does not define constructors, the compiler provides a default constructor with no
parameters, and the class's instance variables are initialized to ________.
a) Zero
b) Null
c) Their default values.
d) False

2. Java requires a ________ call for every object that's created.


a) Constructor
b) Destructor
c) Parameterless
d) Parameterized

3. A _________ is a collection of classes with a related purpose.


a) Package
b) Import
c) Method
d) Collection

4. Instance variables that are object references are initialized to what default value?
a) Empty
b) Instance variables are not initialized to a default value.
c) Null
d) Nil

5. ________ variable belongs to a class, not for each object.


a) Final
b) Static
c) Static final
d) Private

6. Which is the way to initialize Static variables?


a) Do nothing; just declare them.
b) Use an explicit initializer.
c) Use a static initialization block.
d) All of the above

1|Page www.MoussaAcademy.com

00201007153601
7. A correct constructor header for the Student class is:
a) public StudentConstructor()
b) public Student()
c) public void Student()
d) private int Student()

8. Instance variables belong to_________


a) A class
b) An object
c) A method
d) A constructor

9. Which of the following is not a classes names from real life?


a) BankAccount
b) CashRegister
c) Student
d) Math

10. What will be the output of the following Java code?


class overload {
int x;
int y;
void add(int a) {
x = a + 1;
}
void add(int a, int b){
x = a + 2;
}
}
class Overload_methods{
public static void main(String args[]) {
overload obj = new overload();
int a = 0;
obj.add(8);
System.out.println(obj.x);
}}
a) 5
b) 6
c) 9
d) 8

2|Page www.MoussaAcademy.com

00201007153601
11. Overriding a method differs from overloading a method because:
a) Overloaded methods have the same signature.
b) Overridden methods have the same signature.
c) Both of the above.
d) Neither of the above.

12. Which of these operators is used to allocate memory for an object?


a) create
b) realloc
c) new
d) alloc

13. What will be the output of the following Java code?


class ClassConst{
ClassConst (){
System.out.print("hello b");
}
ClassConst(String x){
this();
System.out.print("hello a");
}
}
public class Test{
public static void main(String args[]){
ClassConst a = new ClassConst("hello c");
}
}

a) hello a
b) hello b hello a
c) hello c
d) hello b

14. Java constructor overloading is a technique in which a class can have _________ constructor(s).
a) two
b) one
c) three
d) any number of

3|Page www.MoussaAcademy.com

00201007153601
15. What is the process of defining two or more methods within same class that have same name but
different parameters declaration?
a) method overloading
b) method overriding
c) method hiding
d) none of the mentioned

16. Which of these can be overloaded?


a) Methods
b) Constructors
c) All of the mentioned
d) None of the mentioned

17. What will be the output of the following Java code?


class San{
public void m1 (int i,float f){ System.out.println(" int float method"); }
public void m1(float f,int i); {
System.out.println("float int method");
}
public static void main(String[]args){
San s=new San();
s.m1(20,20);
}}
a) int float method
b) float int method
c) compile time error
d) run time error

4|Page www.MoussaAcademy.com

00201007153601
18. What will be the output of the following Java code?
class overload {
int x; int y;
void add(int a){
x = a + 1;
}
void add(int a , int b){
x = a + 2;
}}
class Overload_methods {
public static void main(String args[]){
overload obj = new overload();
int a = 0;
obj.add(6, 7);
System.out.println(obj.x);
}
}
a) 6
b) 7
c) 8
d) 9

19. What will be the output of the following Java code?


class overload {
int x; double y;
void add(int a , int b) {
x = a + b;
}
void add(double c , double d){
y = c + d;
}
overload() {
this.x = 0;
this.y = 0; } }
class Overload_methods {
public static void main(String args[]){
overload obj = new overload();
int a = 2; double b = 3.2;
obj.add(a, a);
obj.add(b, b);
System.out.println(obj.x + " " + obj.y);
}}
a) 6 6

5|Page www.MoussaAcademy.com

00201007153601
b) 6.4 6.4
c) 6.4 6
d) 4 6.4

20. What will be the output of the following Java code?


class test {
int a; int b;
void meth(int i , int j) {
i *= 2; j /= 2;
}}
class Output {
public static void main(String args[]){
test obj = new test();
int a = 10; int b = 20;
obj.meth(a , b);
System.out.println(a + " " + b);
}}
a) 10 20
b) 20 10
c) 20 40
d) 40 20

21. What will be the output of the following Java code?


class test {
int a; int b;
test(int i, int j) {
a = i; b = j;
}
void meth(test o) {
o.a *= 2; o.b /= 2;
}}
class Output {
public static void main(String args[]){
test obj = new test(10 , 20);
obj.meth(obj);
System.out.println(obj.a + " " + obj.b);
}}
a) 10 20
b) 20 10
c) 20 40
d) 40 20

6|Page www.MoussaAcademy.com

00201007153601
22. If a variable is declared as private, then it can be used in_________.
a) Any class of any package.
b) Any class of same package.
c) Only in the same class.
d) Only subclass in that package.

23. What will be the output of the following Java program?


class A {
int i; int j;
A() {
i = 1; j = 2;
}}
class Output {
public static void main(String args[]){
A obj1 = new A();
A obj2 = new A();
System.out.print(obj1.equals(obj2)); } }
a) false
b) true
c) 1
d) Compilation error

24. What will be the output of the following Java code?


enum Season {
WINTER, SPRING, SUMMER, FALL;
}
System.out.println(Season.WINTER.ordinal());
a) 0
b) 1
c) 2
d) 3

25. What will be the output of the following Java code snippet?
enum Levels {
private TOP, public MEDIUM, protected BOTTOM;
}
a) Runtime Error
b) EnumNotDefined Exception
c) It runs successfully
d) Compilation Error

7|Page www.MoussaAcademy.com

00201007153601
26. Which among the following best describes constructor overloading?
a) Defining one constructor in each class of a program
b) Defining more than one constructor in single class
c) Defining more than one constructor in single class with different signature
d) Defining destructor with each constructor

27. Does constructor overloading include different return types for constructors to be overloaded?
a) Yes, if return types are different, signature becomes different.
b) Yes, because return types can differentiate two functions.
c) No, return type can’t differentiate two functions.
d) No, constructors don’t have any return type

28. Which among the following function can be used to call default constructor implicitly in java?
a) this()
b) that()
c) super()
d) sub()

29. Which keyword should be used to declare static variables?


a) static
b) stat
c) common
d) const

30. Any changes made to static data member from one member function __________
a) Is reflected to only the corresponding object.
b) Is reflected to all the variables in a program.
c) Is reflected to all the objects of that class.
d) Is constant to that function only

31. Which is the correct syntax for declaring static data member?
a) static mamberName dataType;
b) dataType static memberName;
c) memberName static dataType;
d) static dataType memberName;

8|Page www.MoussaAcademy.com

00201007153601
32. The static data member ____________
a) Can be accessed directly.
b) Can be accessed with any public class name.
c) Can be accessed with dot operator.
d) Can be accessed using class name if not using static member function

33. Which of the following statement creates an object of Student class called MyStudent?
a) Student MyStudent = new Student():
b) Students Mystudent ()= new Student ();
c) Student = new MyStudent ();
d) MyStudent Student = new MyStudent ();

34. What is the output of the following statement?


System.out.println("hello".compareTo("goodbye"));
a) -1
b) Not equals.
c) 1
d) 0

35. What is the output of the following code snippet?


int value = 4/2+3-9+2*10;
System.out.println(value);
a) -80
b) 16
c) 30
d) -20

36. Which of the following creates an empty multidimensional array with dimensions 2×2?
a) int [] [] blue = new int[2, 2];
b) int [] [] blue = new int[2], [2];
c) int [] [] blue new int [2] [2];
d) int [] [] blue = new int[2 x 2];.

37. An/A ___________ is a template from which objects are created.


a) variable
b) class
c) array
d) method.

9|Page www.MoussaAcademy.com

00201007153601
38. ________ converts early assembly-language programs to machine language at computer speeds.
a) Editor
b) Compiler
c) Assembler
d) Executer

39. Which of the following is a correct enum declaration?


a) public enum { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
SATURDAY };
b) public enum Week = new enum {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
FRIDAY, SATURDAY }
c) public enum Week = { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
SATURDAY }
d) public enum Week { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
SATURDAY }

40. The java compiler ignores any text between _________.


a) {* and *}
b) /* and */
c) (* and *)
d) &* and *&

41. ___________ concept means one object acquires all the properties and behaviors of a parent object.
a) Modularity
b) Inheritance
c) Polymorphism
d) Encapsulation

42. What is the output of the following java statement?


System.out.println(Pattern.matches("..x","max"));
a) -1
b) true
c) 0
d) false.

10 | P a g e www.MoussaAcademy.com

00201007153601
43. A recursive method is terminated only if it has a ________.
a) for loop
b) base case
c) while loop
d) break statement

44. What is the output of the following code snippet?


int n = 5;
while (n >3) {
System.out.print(n);
n--;
}
a) 54
b) 43
c) 5
d) 543

45. In inheritance, __________ keyword is used by the child class to access parent's variables, methods
and constructors.
a) super
b) extends
c) implements
d) protected

46. A class that represents the most general entity in an inheritance hierarchy is called a/an ________.
a) Superclass
b) Subclass
c) Inheritance class
d) Default class

47. What is the valid range of index values for an array of size 30?
a) 0 to 29
b) 1 to 30
c) 1 to 29
d) 0 to 30

11 | P a g e www.MoussaAcademy.com

00201007153601
48. Which of the following is incorrect about the constructor methods?
a) It has a return value.
b) It can take any number of parameters.
c) It can be used to initialize an object of a class when the object is created.
d) It has the same name as its class.

49. What is the output of the following code snippet?


int firstNumber = 5;
int secondNumber = 5;
firstNumber = secondNumber++;
System.out.println(firstNumber +" && "+ secondNumber);
a) 5 && 6
b) 3 && 5
c) 6 && 6
d) 3 && 6

50. With reference to below java statement, choose the incorrect description of it.
final int FEE = 100;
a) It indicates that variable FEE is not modifiable.
b) You can change the variable's value but not the variable's name.
c) It declares variable FEE as a constant.
d) Any attempt to modify final variable results in error

51. Two methods having the same name with different number and types of parameters in a class are
known as __________.
a) method overloading
b) method overriding
c) method constructing
d) method duplicating

52. Which of the following is the correct way to define a static variable in Java?
a) private final int myNumber = 10;
b) private static int myNumber = 10;
c) private static int myNumber = "10";
d) private int myNumber = 10;

12 | P a g e www.MoussaAcademy.com

00201007153601
53. What is the output of the following java program?
class A{
private int x =100;
}
public class B{
public static void main(String[] args) {
A a= new A();
System.out.println(a.x);
}
}
a) There will be a run-time error.
b) There will be a compile-time error.
c) 100
d) a.x

54. In java, next() method is used to read __________.


a) a line from the user
b) a double value from the user
c) a word (until any white space)
d) an int value from the user.

55. What is the output of the following java statement?


System.out.println(Character.isDigit('2'));
a) 2
b) ‘2’
c) false
d) true

56. What is the output of the following code snippet?


String name="ali";
if (name == "ali")
System.out.print("ALI ");
System.out.println("GOOD");.
a) There will be a compile-time error.
b) GOOD
c) ALI
d) ALI GOOD

13 | P a g e www.MoussaAcademy.com

00201007153601
57. What is the output of the following program?
class FinalTest {
int x =10;
FinalTest (int x) {
this.x = x;
}}
public class FinalTest2 {
public static void main(String args[]) {
FinalTest obj = new FinalTest (20);
System.out.print(obj.x); }
}
a) 20
b) There will be a compile-time error.
c) There will be no output.
d) 10

58. Variables declared inside the methods are always __________ variables.
a) void
b) global
c) public
d) local

59. In the String class, the _________ is used to compare the equality of two string values.
a) isEquals()
b) equals()
c) Equals()
d) IsEquals()

60. What is the output of the following java statement?


System.out.printf("%S!%S%n", "1", "exam");
a) 1exam
b) exam
c) %sSn1EXAm
d) 1!EXAM

14 | P a g e www.MoussaAcademy.com

00201007153601
61. Which is the correct java code to print the following array declaration using enhanced for loop?
int[] arr = new int[4];.
a) for (int x: arr) System.out.print(x);
b) for (int arr: x) System.out.print(x);
c) for (String x: arr) System.out.print(x);
d) for (int x = arr) System.out.print(x);

62. When a method calls itself, it would be defined as a ___________ method.


a) repeated
b) public
c) recursive
d) loop

63. What is the output of the following code snippet?


int[] odd = {1, 3, 5, 7, 9, 11 };
odd [3] = 13; odd[4] = 77;
System.out.println(odd[0] + “ “+ odd [3]);.
a) 1 5
b) 1 13
c) 6
d) 14

64. You can get the values of private variables from outside its class by using ____________.
a) private get method
b) private set method
c) public set method
d) public get method

65. Which of the following is an incorrect statement about arrays?


a) An array is a reference typed variable.
b) An array can expand its size automatically when it is full.
c) An array can contain duplicate values.
d) The first index of an array is always zero.

15 | P a g e www.MoussaAcademy.com

00201007153601
66. To create an object, you use a _______ keyword.
a) String[]
b) int[]
c) new
d) create

67. Which of the following is NOT the selection control structure?


a) for
b) if.else
c) if
d) switch

68. If no constructors are provided in a class, the compiler creates ___________.


a) a default constructor
b) an overloaded constructor
c) an infinite constructor
d) a main constructor

69. A variable that is declared inside a class but outside anything else such as a constructor or a method is
known as _________ variable.
a) private
b) public
c) local
d) instance

70. ________ variable is created at runtime and is shared among the agents at the class level.
a) local
b) private
c) public
d) static

71. The following code will print Welcome for _________ times.
for (int i = 1; i < 20; i = i*2) {
System.out.println("Welcome");}
a) Seven
b) Five
c) two
d) three

16 | P a g e www.MoussaAcademy.com

00201007153601
72. Consider the following java classes: Account and Main.
The account class is already defined for you.
Write the following code inside the main method in the main class.
• Create a new object of the account class.
• Use the set methods in the account class to set a new account ID, an amount of money, and You can
select any values you like.
Use the get methods to get all the values that you just created (i.e., account ID, amount of the money
and print them using the println() method.
Account.java
public class Account
private int accountID:
private int amount;
private String name;
// method that sets the account ID
public void setAccountID (int accountID) {
this.accountID = accountID; }
// method that sets the amount
public void setAmount (int amount) {
this.amount amount; }
// method that sets the name
public void setName (String name) {
this.name = name; }
public int getAccountID() {
return account ID; }
public int getAmount () {
return amount; }
public String getName () {
return name; }
}
Main.java
public class Main {
public static void main(String[] args) {
// write your code here.
// Create an object of the Account class
Account myAccount = new Account();

// Set values using the setter methods


myAccount.setAccountID(123456);
myAccount.setAmount(1000);
myAccount.setName("Mohamed Ahmed");

// Retrieve values using the getter methods and print them


System.out.println("Account ID: " + myAccount.getAccountID());
System.out.println("Amount: " + myAccount.getAmount());
System.out.println("Name: " + myAccount.getName());
}}

17 | P a g e www.MoussaAcademy.com

00201007153601
73. Write a Java statement (not a full program) to accomplish the following task:
1. Define a string variable named my_name that stores your first name.
2. Find and print the number of characters present in the string my_name using the length method in
the String class.
3. Replace your first name with your last name using the assignment operator and print the result.

// 1. Define a string variable named my_name that stores your first name.
String my_name = "YourFirstName";

// 2. Find and print the number of characters present in the string my_name using the length method
in the String class.
int numOfChars = my_name.length();
System.out.println("Number of characters: " + numOfChars);

// 3. Replace your first name with your last name using the assignment operator and print the result.
String lastName = "YourLastName";
my_name = lastName;
System.out.println("Updated name: " + my_name);

18 | P a g e www.MoussaAcademy.com

00201007153601
JAVA Final 2023
53 Questions

Moussa Academy
00201007153601
WWW.MOUSSAACADEMY.COM
Java 1 Final Winter 2023
JAVA 1
Answers

Reviewed by:
Mamdouh Moussa

Moussa Academy
WWW.MOUSSAACADEMY.COM
Java 1 Final Winter 2023 (‫(الاجابات في نهاية المذكرة‬

1. The class which inherits the properties of other class is known as

A) superclass

B) parent class

C) base class

D) subclass

2. Which of the following methods is used to invoke the current class constructor

A) final ()

B) this()

C) enum()

D) catch()

3. The Java compiler ignores any text enclosed between and the end of the line.

A) ( )

B) ‘. ‘

C) //

D) " “

4. To access the features of a package in Java, you would use the

A) export

B) utilize

C) use

D) Import

5. What is the output of the following Java code snippet?


String data = "Hello World!";
System.out.printf("Printing a string: %S\n", data);

A) PRINTING A STRING: HELLO WORLD!

B) Printing a string: HELLO WORLD!

C) HELLO WORLD!

D) Hello World!

1
6. In a Multi-Level Inheritance in Java, the last subclass inherits methods and properties of

A) only one immediate superclass

B) few classes above it

C) none of the classes above it

D) all classes above it

7. What does the following code snippet do?


String value = "Work hard!";
String[] split = value.split (" ");

A) Divide the string into sub-strings

B) Remove white spaces

C) Unify the string with another string

D) Change the String type into the char type

8. What is the output of the following Java code snippet?


String value 1 = "Work hard "
String value 2 = "to pass "
String value 3 = "the exam "
System. Out. printIn ( value1 + value2 + value3)

A) The exam

B) Work hard to pass

C) to pass the exam!

D) Work hard to pass the exam!

9. Information can be passed to a method as parameters, which are specified after the function name,
inside the.....................and separated with a comma.

A) square brackets [ ]

B) braces { }

C) parentheses ( )

D) angle brackets <>

2
10. What is the output of the following Java code snippet?
int x = 0;
x = 50;
x = 100;
x += 45;
System.out.println(x);

A) 145

B) 50

C) 45

D) 100

11. In Java, a collection of related types (classes and interfaces) that provides namespace management
and access control

A) interface

B) lang

C) package

D) object

12. In Java, the private, public & protected are:

A) objects

B) classes

C) interfaces

D) access modifiers

13. is the action of a function that calls itself either directly or indirectly, where the associated me
known as method.

A) Lang, lang

B) Recursion, recursive

C) Translation, transitive

D) Interface, interface

14. In OOP, a/an................... is group of objects which have common properties. Moreover, it is a template
or blueprint of objects are created.

A) class

B) polymorphisme

C) abstract

D) interface

3
15. Which of the following Java code lines declares and initializes an integer array of size

A) int a [] = new double [8];

B) int a [] =new int [8] ;

C) int all = new int [8];

D) int [] a = new int [1, 2,3,4];

16. The keyword indicates that the method should not return a value.

A) return

B) break

C) void

D) private

17. Which one of the following is the keyword that is used to implement an interface in Java?

A) implements

B) super

C) extends

D) Final

18. What is the valid range of index values for an array of size 20?

A) 0 to 20

B) 1 to 20

C) 0 to 19

D) 1 to 19

19. If a class has multiple methods having it is known as Method Overloading.

A) different names and different parameters

B) same name but different in parameters

C) different in name but the same parameters

D) same name and the same parameters

20. The constructor method has the ......... name as its class and has .......... return type.

A) different, double

B) same, no explicit

C) same, int

D) different, String

4
21. Which of the following is a correct statement used to create two new Java variables in one line?

A) String fName: 1 Name;

B) String fName: String IName;

C) String fame, lName;

D) String fame, String lName;

22. R Questi Which of the following system software provides services that enable each program to
function concurrently (i.e., in parallel) with programs in a secure and efficient manner?

A) Distributed systems

B) Time sharing systems

C) Operating systems

D) Log file

23. Which of the following is the fundamental building block that must exist at least once in every Java
program

A) Class

B) Variable

C) CPU

D) Applet

24. A class can have references to one or more objects of other classes as members. This is called

A) overlapping

B) inheritance

C) overloading

D) composition

25. What is the output of the following Java code segment?if (1>3);

A) Know Program

B) Compilation Error

C) No output

D) 3

26. The is formed when a loop exists inside the body of another loop.

A) do/while loop

B) sentinel loop

C) nested loop

D) parallel loop

5
27. The predefined methods can be defined as methods that are

A) friends-defined methods

B) user-defined methods

C) internet-defined methods

D) already exist in the Java class libraries

28. What is the output of the following Java statement? System.out.println(Pattern.matches(".s", "as"));

A) false

B) as

C) True

D) s

29. ................. is needed to terminate the recursion.

A) break statement

B) base case

C) stop statement

D) void return type

30. The type is a special class that defines a set of constants represented as unique identifiers.

A) int

B) Char

C) enum

D) Boolean

31. In Java, the Character method isLetter0rDigit

A) determines if a character can be used in an identifier

B) determines whether a character is a letter

C) determines if a character is a letter or a digit

D) converts a character to its lowercase equivalent

32. programs allowed for the direct execution of high-level language programs without them to first be
translated into a machine language.

A) Abstraction

B) pointers

C) Exception

D) interpreter

6
33. What is the output of the following Java code snippet? char a = '6': system. Out. Println(
Character.isLetter(a)):

A) true

B) a

C) 6

D) false

34. The ........................ provides a default constructor that accepts no parameters when it is called if you
do not specify any in the declaration of a class.

A) compiler

B) recursion

C) composition

D) iteration

35. What is the output of the following Java code snippet? double x = 2; double y. = 3; double z = 1 - x
* Math. pow (x, 3) + (Math. pow (y, 2) + x) ; System.out.println (z);

A) -31.0

B) -4.0

C) 3

D) -37.0

36. Which of the following provides a code editor, a compiler or interpreter, and a debugger that the
developer can use through a graphical user interface?

A) Java Constructor (JC)

B) Class Library Reference (CLR)

C) Java Package (JP)

D) Integrated Development Environments (IDEs)

37. Given the following array, which of the following lines inserts the value 10 in the last element of the
int [] arr= new int [15];

A) arr [10]=15;

B) arr [15]=10;

C) arr [15] [15-1]=10;

D) arr [14]=10;

7
38. What is the output of the following Java code segment?
int[] x = new int[3];
System.out.println("x[0] is " + x[1]);

A) x[0] is 3

B) x[0] is 0

C) Compilation Error

D) x[0] is 1

39. Which of the following is a keyword that acts as a non-access modifier for classes, and methods, in
unchangeable (not inheritable or overridable)?

A) assert

B) extends

C) final

D) default

40. What is the output of the following snipped code segment?


int[] a = new int[2];
System.out.println(a.length);

A) 0

B) 2

C) 3

D) 1

41. Java arrays are essentially

A) objects

B) class variables

C) instance variables

D) primitive data types

42. Which one of the following is the keyword that is used to inherit a class?

A) implement

B) extends

C) extent

D) this

8
43. Which of the following is the correct word used in the statement of creating an object of the Car
class >
Car c = ____________Car ( )?

A) create

B) run

C) class

D) new

44. Which of the following is the correct string format for System. out .printf () to print an integer with a
width of 15?

A) "%15.0d"

B) "%15rd"

C) *%15d"

D) *%15r"

45. What is the output of the following Java program?

public class PolymorphismX {

void ml(String x) {
System.out.print("One ");
}

protected void ml(String x, String y) {


System.out.println("Two");
}

public static void main(String[] args) {


PolymorphismX obj = new PolymorphismX();
obj.ml("ABC");
obj.ml("PQR", "XYZ");

}
}

A) Compilation Error

B) One Two

C) Two One

D) Runtime Error

9
46. What is the output of the following Java program?

public class Main {

public static void main(String[] args) {


try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[1]);
} catch (Exception e) {
System.out.println("Something went wrong.");
}
}
}

A) 1

B) Something went wrong.

C) 2

D) 2 Something went wrong.

47. What is the output of the following Java code snippet?

int firstNo=45;
int secondNo=60;
firstNo=secondNo--;
System.out.println(firstNo + " & " + secondNo);

A) 60 & 59

B) 45 & 59

C) 45 & 60

D) 60 & 60

48. What is the output of the following Java program?


public class Main {
public static void main(String[] args) {
char[] letters={'j','a','v','a'};
for(char element:letters)
System.out.print(element+ " ");
}
}

A) e l e m e n t

B) Compile error

C) j a v a

D) throw a Run time error

10
49. What is the output of the following Java program?
public class ABC {
private int num=100;
}

public class FinalExam {


public static void main(String[] args) {
ABC obj=new ABC();
System.out.println(obj.num);
}
}

A) Run time error

B) 100

C) compile time error

D) obj.num

50. What is the output of the following Java code snippet?

int arr[][]={{7,8,9},{4,5,6},{1,2,3}};
int sum=0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum=sum+arr[i][j];
}
}
System.out.println(sum);

A) 45

B) 11

C) 40

D) 10

11
51. What is the output of the following Java code snippet?

public class Main {


public static void main(String[] args) {
int [][] evenArr={
{2,4,8},
{8,10,12}
};

System.out.print(evenArr[0][0]);
System.out.print(evenArr[1][0]);

}
}

A) 410

B) 26

C) 28

D) 612

52. What is the output of the following Java code snippet?

char[] array={'T','h','i','s',' ','i','s',' ','S','E','U'};


String value=new String(array);
for (int i = 8; i < value.length(); i++) {
System.out.print(value.charAt(i));
}
System.out.println("");

A) This

B) is

C) This is SEU

D) SEU

12
53. What is the output of the following Java program?

public class Main {

public static void main(String[] args) {


for (int count = 3; count>=0; count--) {
if(count==2)
break;
System.out.print(count+" ");
}
}
}

A) 2

B) 3

C) 0

D) No output

13
Answer Keys

Question Answer Question Answer

1 D 28 C
2 B 29 B
3 C 30 C
4 D 31 C
5 B 32 D
6 D 33 D
7 A 34 A
8 D 35 B
9 A 36 D
10 A 37 D
11 C 38 D
12 D 39 C
13 B 40 B
14 A 41 A
15 B 42 B
16 C 43 D
17 A 44 C
18 C 45 B
19 B 46 A
20 B 47 A
21 C 48 C
22 C 49 A
23 A 50 A
24 D 51 C
25 C 52 D
26 C 53 B
27 D

14
JAVA Quiz Fall 2023
47 Questions

Moussa Academy
00201007153601
WWW.MOUSSAACADEMY.COM
Java 1 – Solved Exam Quiz Fall 2023

Moussa Academy
00201007153601
WWW.MOUSSAACADEMY.COM
I. Multiple Choice Questions

1. If a method contains a local variable with the same name as an instance variable, the method's body
can use the keyword ____________ to refer to the shadowed instance variable explicitly.
a) public
b) shadow
c) private
d) this

2. Instance variables of type long is initialized by default to ______________ .


a) 1

b) False
c) 0
d) Null

3. What is the output of the following code?


class Calculate{
static int cube (int x){
return x * x * x;
}

public static void main(String args[]){


int result = Calculate.cube(3);
System.out.println(result);
}
}

a) Syntax Error
b) 9
c) 27
d) Runtime Error

1|Page www.MoussaAcademy.com

00201007153601
4. How many "int” values can be stored in a single element of an int type array?
a) 1
b) Unlimited
c) 10
d) 2

5. Which of the following method signatures is not considered an example of method overloading when
compared to the others?
a) static double add(double a double b)
b) static int add(int a, int b)
c) static double add1(int a, int b)
d) static int add(int a, int b, int c)

6. ________________ is defined as several methods of the same name, each with different parameters.
a) Method Overloading
b) Method Generalization
c) Method covering
d) Method Parameterization

7. What is the output of the following code?


for (int j = 0; j < 5; j++) {
System.out.print( j + " ");
}

a) 1 2 3 4 5
b) j j j j j
c) 0 1 2 3 4 5
d) 0 1 2 3 4

8. A/an _______________ would be used to have a loop that is inside another loop?
a) Nested loop
b) Nested if statement.
c) If statement
d) Method

2|Page www.MoussaAcademy.com

00201007153601
9. Consider the following Java code snippet:
int num1 = 10;
double num2 = 7.5;
int result = _____ num2 + num1;

What should replace the blank space to correctly compile and execute the code?

a) /
b) +
c) -
d) (int)

10. In Java, which of the following is an incorrect method declaration?


a) public String, int getNameNumber()
b) public int getNumber()
c) public void setName()
d) public String getName()

11. What is the output of the following code?

public class Main {


public static void main(String[] args) {
System.out.println(print(5));
}

public static double print(int i) {


return i;
}
}

a) 5
b) 5.0
c) Error
d) Five

3|Page www.MoussaAcademy.com

00201007153601
12. What is the output of the following code?

public class Test {


static int x = 8;

public static void main(String[] args) {


int x = 5;
for (x = 0; x < 10; x++) {
// some statement
}
System.out.println(x);
}

public static void foo() {


System.out.println(x);
}
}

a) 10
b) 5
c) 8
a) 15

13. Which of the following statements about the Java switch statement is true?
a) The "break" statement is required for every case in a switch statement
b) The switch statement can only be used with integer and character data types.
c) The "default” case in a switch statement is optional.
d) You can use a switch statement to compare complex data types, such as strings

14. If you want to create an if statement to verify if an integer variable myVar equals 40. Which condition
is correct?
a) if (myVar = = 40)
b) if ((myVarl 40) && (myVar !> 40))
c) if (myVar - 40 = 0).
d) if (myVar = 40)

4|Page www.MoussaAcademy.com

00201007153601
15. What is the output of the following code?

package abcpackage;

class Addition {
public int addTwoNumbers(int a, int b) {
return a + b;
}
}

package xyzpackage;
import abcpackage.*;

public class Test {


public static void main(String args[]) {
Addition obj = new Addition();
System.out.println(obj.addTwoNumbers(10, 10));
}
}

a) Compile time Error


b) Syntax Error
c) 21
d) 20

16. What does the "break" statement do in a Java switch statement?


a) It transfers control to the default case.
b) It ends the execution of the entire method containing the switch statement.
c) It exits the current case and continues to the next case.
d) It terminates the execution of the entire switch statement and any remaining cases

17. Instance variable in Java is created ____________.


a) Outside the class but inside the method.
b) Inside the class but outside the method.
c) Inside the class and inside the method.
d) Outside the class and outside the method

18. In Java, which of the following is a correct method identifier?


a) changeNumber
b) change.Number
c) 123changeNumber
d) change Number

5|Page www.MoussaAcademy.com

00201007153601
19. What is the output of the following java code?

public class ABC {


public static void main(String[] args) {
char grade = 'L';
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
case 'C':
System.out.println("Well done");
break;
case 'D':
System.out.println("You passed");
break;
case 'F':
System.out.println("Better try again");
break;
default:
System.out.println("Invalid grade");
}

}
}

a) Excellent!
b) Well done.
c) Invalid grade
d) Great

20. Variables or methods declared with access modifier ____________ are accessible only to methods of
the class in which they're declared.
a) Public and private
b) Public
c) Public and default
d) Private

21. Which do you access an element in a one-dimension in Java?


a) arrayLength()
b) lengthOfArray()
c) length()
d) getSize()

6|Page www.MoussaAcademy.com

00201007153601
22. In Java, what is the main purpose of a nested if statement?
a) To create loops.
b) To execute a block of code conditionally within another if statement.
c) To define custom data types.
d) To compare two variables

23. What is the default initial value for elements in an array of integers in Java?
a) 0
b) -1
c) null
d) 1

24. Array index starts from _____________ .


a) 0
b) 1
c) Any number
d) -1

25. The __________ is used to immediately exit from a for statement.


a) block
b) break
c) continue
d) terminate

26. In Java, what does the "void" return type indicate for a method?
a) It returns a boolean value
b) It returns an integer
c) It does not return any value
d) It returns a String value

7|Page www.MoussaAcademy.com

00201007153601
27. What is the requirement regarding the data types of elements inside an array?
a) They must all be of the same type
b) They must be from related types
c) They must be from different types.
d) They must be only strings.

28. A loop inside another loop is known as ______________.


a) While loop
b) Nested loop
c) Do while loop
d) For loop

29. What value is stored in num at the end of this looping?


for (num = 0; num <= 5; num++);

a) 6
b) 1
c) 4
d) 5

30. In Java, “int” in the method declaration, public int find Sum(), is called a/an ____________ .
a) Parameter
b) Return type
c) Access Modifier
d) Identifier

31. What is the highest index in an array of size n in Java?


a) 0
b) n
c) n - 1
d) n + 1

32. What is true about casting in Java?


a) Casting is the process of converting one data type to another.
b) Casting is a way to create new objects in Java.
c) Casting is only used when defining custom exception classes in Java.
d) Casting is a mechanism for defining access modifiers in Java classes

8|Page www.MoussaAcademy.com

00201007153601
33. What is the output of the following java code?
public class Example {
static int x = 8;

public static void main(String[] args) {


int x = 5;
for (x = 0; x < 10; x++) {
// some statement
}
foo();
}

public static void foo() {


++x;
System.out.println(x);
}
}

a) 8
a) 9
b) 10
c) 5

34. How should the condition be filled in the blank to make the following code print the integers from 5
through 15, inclusive?

for (int j = 5; ______ ; j++) {


System.out.print(j + " ");
}
System.out.println();

a) j<=16
b) j<16
c) j<15
d) j==15

35. Which of the following loops ensures that its body is executed at least once?
a) for
b) do-while
c) for and while
d) while

9|Page www.MoussaAcademy.com

00201007153601
36. What type of loop in Java is best suited for situations where we know the repetition number?
a) do-while and while
b) while
c) do-while
d) for

37. How many times does the following loop execute?


int items = 5;
while (items >= 5) {
System.out.println("items are more than 5");
items++;
}

a) The loop never executes.


b) The loop runs forever.
c) 5 times
d) Only once

38. How do you access an element in a one-dimensional array?


a) Using the element's value
b) Using its index or position
c) Using dot notation
d) Using the element's name

39. When does the do-while loop test the loop continuation condition?
a) before or after executing the loop's body
b) before and after executing the loop's body
c) after executing the loop's body
d) before executing the loop's body

40. What term refers to the portion of a program that can reference an entity by its name?
a) Scope of instantiation
b) Scope of declaration
c) Scope of initialization
d) Scope of generalization

10 | P a g e www.MoussaAcademy.com

00201007153601
41. What is required to pass an array to a method in Java?
a) Declare a new array with the same name
b) The name of the array without brackets
c) The array cannot be passed from another method
d) The name of the array with brackets

42. In Java, “int a” in the method declaration, public void print(int a), is called a/an___________.
a) Identifier
b) Access modifier
c) Parameter
d) Return type

43. What is the output of the following code?

String s = 5 < 4 ? "TRUE" : "FALSE";


System.out.println(s);

a) FALSE
b) TRUE.
c) TRUE: FALSE
d) Runtime error

44. The ______ package contains classes and interfaces for enabling data input and output.
a) Java.io
b) Javax.swing
c) Java.util
d) Java.lang

45. _______________ is/are defined as Classes grouped into categories of related classes.
a) Java programs
b) Applications
c) Packages
d) Overloading

11 | P a g e www.MoussaAcademy.com

00201007153601
46. What is the output of the following java code?

package abcpackage;

class Addition {
private int addTwoNumbers(int a, int b) {
return a + b;
}
}

package xyzpackage;
import abcpackage.*;

public class Test {


public static void main(String args[]) {
Addition obj = new Addition();
System.out.println(obj.addTwoNumbers(5, 2));
}
}

a) Syntax Error
b) Compile time Error
c) 6
d) 7

47. What is the output of the following java code?

public class Example {


static int x = 8;

public static void main(String[] args) {


for (int x = 0; x < 10; x++) {
// some statement
}
System.out.println(x);
}
}

a) 8
b) 5
c) 10
d) 9

12 | P a g e www.MoussaAcademy.com

00201007153601

You might also like