Exam Questions
Exam Questions
3) Which keyword helps to distinguish between local variable and variables passed in the
method as parameters having the same name?
a. this
b. new
c. constructor
https://www.sanfoundry.com/java-questions-answers-freshers-experienced/
4) What is the result of this operation in Java ; (int)(7.9)?
a. 7
b. Syntax error
c. 8
Print the output of this code and explain why did you get this output
class Student{
int id;
String name;
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
0
Null
Default constructor
Question 3: ( 6 Marks)
Use the following class to find the area of 2 rectangles r1 and r2:
class Rectangle{
int length;
int width;
void insert(int l, int w){
length=l;
width=w;
}
void calculateArea(){System.out.println(length*width);}
}
class TestRectangle1{
public static void main(String args[]){
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
وزارة التعليم العالي Subject Title: Artificial Intelligence
Subject Code: 213كهت Academic Year: 2024/2025
Department: Computers
Year: 2 Semester: 1
Date: 23 /10 /24 Time Allowed: ( 0.5 ) Hours.
No. of Pages: () Total Grade: ( ) Marks.
Dr. May Salama
Examiner:
Quiz 1 2024-2025
معهد الصفوة العالي للهندسة
Instructions: Answer All the Following Questions. Competencies achieved ( )
8) Which keyword helps to distinguish between local variable and variables passed in
the method as parameters having the same name?
d. this
e. new
f. constructor
https://www.sanfoundry.com/java-questions-answers-freshers-experienced/
9) What is the result of this operation in Java ; (int)(7.9)?
d. 7
e. Syntax error
f. 8
https://www.placementpreparation.io/mcq/java/
Question 2: ( 4 Marks)
Print the output of this code and explain why did you get this output
class Student{
int id;
String name;
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
0
Null
Default constructor
Question 3: ( 6 Marks)
Use the following class to find the area of 2 rectangles r1 and r2:
class Rectangle{
int length;
int width;
void insert(int l, int w){
length=l;
width=w;
}
void calculateArea(){System.out.println(length*width);}
}
https://www.javatpoint.com/object-and-class-in-java
class TestRectangle1{
public static void main(String args[]){
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
Competencies
Q1 Q2 Q3 Q4
Good Luck.
Q 1 - What is correct syntax for main method of a java class?
A - static
B – Boolean
C - void
D - private
A - A class is a blue print from which individual objects are created. A class can
contain fields and methods to describe the behavior of an object.
A - True
B – False
A - True
B – False
Q 9 - What of the following is the default value of an instance variable?
A - null
B-0
D - Not assigned
A – True
B - False
A - True
B – False
A - Instance variables are static variables within a class but outside any method.
C - Instance variables are variables within a class but outside any method.
A - class variables are static variables within a class but outside any method.
C - class variables are variables within a class but outside any method.
1- What does this code do?
Code explanation:
The code Examines an array of floats and returns true if the values are ordered in
ascending order, allowing for duplicate values. Otherwise it returns false.
PARAMETERS:
a The integer array to be verified.
RETURNS: true if the array is sorted in ascending order,
false otherwise.
ALGORITHM: The algorithm scans the array from left to right and
compares each element with the one to the right of it. If
the element is larger than the next one, then it returns
false. If it reaches the end of the array without having to
return false, then it returns true.
(b) static int numberOfMatches (String a, String b)
{
int n = a.length(), m = b.length();
int i = 0; // i keeps track of how many pairs of cells match
while (i < n && i < m && a.charAt(i) == b.charAt(i))
++i;
return i; // Number of matching characters from the start.
}
Code explanation:
Compares two strings to determine how many of the initial characters match.
PARAMETERS:
a, b The strings to be compared.
RETURNS: The number of non‐NUL initial matches in the two arrays.
For example, if the arrays contain "boasted" and "boats" (with NUL following the
final 'd' and 's' respectively), then 3 will be returned because "boa"
matches "boa".
ALGORITHM: The characters are compared pairwise, starting at cell
0, until a NUL is reached or a pair of characters does
not match.
2- Write Java program to check if the given number is a prime number.
stack.push(i);
stack.pop(); // Remove 9.
stack.pop(); // Remove 8.
stack.pop(); // Remove 7.
stack.peek()= 6
public class Test {
public static void main(String[] args) {
Stack<Integer> stackOne = new Stack<Integer>();
Stack<Integer> stackTwo = new Stack<Integer>();
for (int i = 0; i < 100; i++) // Values divisible by 4 are added to stackTwo.
if (i % 4 == 0)
stackTwo.push(stackOne.peek()); // Take the top element of stackOne.
Question 1: Write True or False for each of the following: (10 Marks)
.
Question 2 ( 6 Marks)
PARAMETERS:
a, b The strings to be compared.
RETURNS: The number of non‐NUL initial matches in the two
arrays. For example, if the arrays contain "boasted" and
"boats" (with NUL following the final 'd' and 's'
respectively), then 3 will be returned because "boa"
matches "boa".
B) static boolean concatenate (int[] a[], int[] b, int[] c)
{
int m = a.length, n = b.length, p = c.length;
if (m + n > p)
return false;
int i, j;
for (i = 0; i < m; ++i)
c[i] = a[i];
for (j = 0; j < n; ++j)
c[i++] = b[j]; // Increment i and j after each assignment
return true;
PARAMETERS:
a, b The arrays from which numbers will be copied into "c".
c The array that will receive the copied numbers.
RETURNS: true, provided the capacity of c is at least equal to the
number of numbers that are to be copied from arrays a
and b; false is returned if this condition fails.
Question 3: (6 Marks)
Compare between java constructor and java method. Give at least 3 differences.
Question 4: ( 8 Marks)
1) The LinkedList class is a collection which can contain many objects of the same type. T
2) The linked list requires movement of nodes when performing deletion and insertion. F
3) In linked list, To reach a particular node, we need to go through all the nodes that come before that
particular node. T
4) Circular linked list nodes consist of three fields: an integer value and two links pointing to two
other nodes (one to the last node and another to the next node). F
5) A priority queue must have all its items ordered. T
6) A queue is a First-In-Last-Out storage mechanism. F
7) A Stack is a data structure that allows elements to be added and removed only from the top of the
stack. T
Question 2: ( 4 Marks)
public class StackTest {
stackOne.push(i);
}}