0% found this document useful (0 votes)
6 views19 pages

Exam Questions

The document is a quiz and exam paper for a course on Artificial Intelligence, detailing questions related to Java programming concepts, including syntax, features, and object-oriented principles. It includes multiple-choice questions, code output explanations, and programming tasks, along with instructions and grading criteria. The paper is structured for the academic year 2024/2025 and is intended for second-year computer science students.

Uploaded by

hishame125
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)
6 views19 pages

Exam Questions

The document is a quiz and exam paper for a course on Artificial Intelligence, detailing questions related to Java programming concepts, including syntax, features, and object-oriented principles. It includes multiple-choice questions, code output explanations, and programming tasks, along with instructions and grading criteria. The paper is structured for the academic year 2024/2025 and is intended for second-year computer science students.

Uploaded by

hishame125
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/ 19

‫وزارة التعليم العالي‬ 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 ( )

Question 1: Select the correct answer ( 5 Marks)

1) Which statement is true about Java?


a) Java is a code dependent programming language
b) Java is a platform-dependent programming language
c) Java is a platform-independent programming language

2) Which one of the following is not a Java feature?


a) Object-oriented
b) Use of pointers
c) Portable

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

5) Identify the error in this code:


int[] nums = new int[2]; nums[0] = 1; nums[1] = 2; nums[2] = 3;
a. No error
b. Array index out of bounds
c. Incorrect array declaration
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);}
}

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 ( )

Question 1: Select the correct answer ( 5 Marks)

6) Which statement is true about Java?


a) Java is a code dependent programming language
b) Java is a platform-dependent programming language
c) Java is a platform-independent programming language

7) Which one of the following is not a Java feature?


a) Object-oriented
b) Use of pointers
c) Portable

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

10) Identify the error in this code:


int[] nums = new int[2]; nums[0] = 1; nums[1] = 2; nums[2] = 3;
d. No error
e. Array index out of bounds
f. Incorrect array declaration

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 - public static int main(String[] args)

B - public int main(String[] args)

C - public static void main(String[] args) 

D - None of the above.

Q 2 - Which of the following is not a keyword in java?

A - static

B – Boolean 

C - void

D - private

Q 3 - What is a class in java?

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. 

B - class is a special data type.

C - class is used to allocate memory to a data type.

D - none of the above.

Q 5 - Objects are stored on Stack.

A - True

B – False 

Q 7 - Can we compare int variable with a boolean variable?

A - True

B – False 
Q 9 - What of the following is the default value of an instance variable?

A - null

B-0

C - Depends upon the type of variable 

D - Not assigned

Q 19 - Can we have multiple classes in same java file?

A – True 

B - False

Q 20 - Can we have two public classes in one java file?

A - True

B – False 

Q 22 - What is instance variable?

A - Instance variables are static variables within a class but outside any method.

B - Instance variables are variables defined inside methods, constructors or blocks.

C - Instance variables are variables within a class but outside any method.

D - None of the above.

Q 23 - What is class variable?

A - class variables are static variables within a class but outside any method.

B - class variables are variables defined inside methods, constructors or blocks.

C - class variables are variables within a class but outside any method.
1- What does this code do?

(a) static boolean isOrdered (int[] a)


{
int n = a.length;
int i;
for (i = 0; i < n‐1; ++i) // Stop before the last element.
// Determine whether a[i] and a[i+1] are in the right order.
if (a[i] > a[i+1]) // not in order
return false;
return true; // If we made it here, the array is in order.
}

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.

public class WhatIsIt {


public static void main(String[] args) {
System.out.println(xXXX(19)); // true
System.out.println(xXXX(49)); // false
}

public static boolean xXXX(int n) {


if (n == 0 || n == 1) { // 0 and 1 are not prime numbers.
return false;
}
if (n == 2) { // 2 is a prime number.
return true;
}
for (int i = 2; i <= n / 2; i++) { // Check divisors.
if (n % i == 0) {
return false;
}
}
return true; // If no divisors are found, it's prime.
}
}
public class StackTest {

public static void main(String[] args) {

Stack<Integer> stack = new Stack<Integer>();

for (int i = 0; i < 10; i++) // Push numbers from 0 to 9.

stack.push(i);

stack.pop(); // Remove 9.

stack.pop(); // Remove 8.

stack.pop(); // Remove 7.

System.out.println(stack.peek()); // Top element: 6.

3-What is the value returned when stack.peek() is called?

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++) // Add values from 0 to 99 to stackOne.


stackOne.push(i);

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.

System.out.println("stackOne size: " + stackOne.size()); // 100


System.out.println("stackTwo size: " + stackTwo.size()); // 25
}
}
4 - How many elements are in each Stack?
stackOne size: 100
stackTwo size: 25
‫وزارة التعليم العالي‬ Subject Title: Artificial Intelligence
Subject Code: ECE 213C Academic Year: 2024/2025
Department: Electronics & Communications & Computers Engineering
Year: 2nd Semester: 1st
Date: /11 /24 Time Allowed: ( 1 ) Hours.
No. of Pages: ( ) Total Grade: ( 30 ) Marks.
Examiner: Dr. May Salama
Mid-Term Exam 2024-2025
‫معهد الصفوة العالي للهندسة‬
Instructions: Answer All the Following Questions. Competencies achieved ( )

Question 1: Write True or False for each of the following: (10 Marks)

1- The ‘javac’ command is used to run a java program. F


2- The output of the following code is 2000 T
int m=1000; int k= 3000;
while(++m< --k); // do nothing
system.out.println(m);
3- The following 2 instructions are BOTH valid methods to create a string in Java T
String S=”Hello Java
String S2=new String(“Hello Java”)
4- A constructor has NO return type. T
5- The search complexity of ordered array is O(n) F(O(log(n)
6- When an array is passed to a method, the method receives a copy of the array. F
7- The object created with new keyword is created at compile time. F
8- There is only one type of java constructor which is the parametrized F
9- The contents of a queue can wrap around, while those of a stack cannot. T
10- Pushing and popping items on a stack and inserting and removing items in a queue all take
O(N) time. F

.
Question 2 ( 6 Marks)

What does each of the following methods do? Explain in detail.


A) 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;
}

DESCRIPTION: 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".
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;

Copies numbers from two arrays into a third array. The


numbers from the second array are placed to the right
of the numbers copied from the first array.

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)

(A) Write a Java program to sum values of an array.


(B) Write a Java program to implement a stack with push and pop
operations. Check if the stack is empty or full.
Quiz
Question 1: Write True or False for each of the following questions ( 7 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 {

public static void main(String[] args) {

Stack<Integer> stackOne = new Stack<Integer>();

Stack<Integer> stackTwo = new Stack<Integer>();

for (int i = 0; i < 5; i++) // Push 0 to 4 into stackOne.

stackOne.push(i);

stackTwo.push(stackOne.pop()); // Pop 4 from stackOne and push to stackTwo.

stackTwo.push(stackOne.pop()); // Pop 3 from stackOne and push to stackTwo.

stackTwo.push(stackOne.pop()); // Pop 2 from stackOne and push to stackTwo.

}}

1- What are the values returned by peek on stackone and stack


two?

Value of stackOne.peek(): 1 (top element of stackOne).

Value of stackTwo.peek(): 2 (top element of stackTwo).


Question 3 :
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++) // Push 0 to 99 into stackOne.


stackOne.push(i);

for (int i = 0; i < 100; i++) // Push values divisible by 4 to stackTwo.


if (i % 4 == 0)
stackTwo.push(stackOne.peek());
}
}

1- How many elements are in stackOne and stackTwo?

Number of elements in stackOne = 100.


Number of elements in stackTwo = 25.

You might also like