Algorithm and Data Structures
Algorithm and Data Structures
SECTION – A ( 4 x 2 = 8 Marks)
Answer ALL Questions. Each Question carries TWO Mark
Q1. What are the types of data that can be used to declare a class attributes?
Answer:
Primitive data type as: short ,int, double ,char, boolean float, long, byte.
Object data type as: String, or any variable defined by a class as: Scanner c, c is
considered as an object.
Answer :
1- Java ArrayList allows duplicate and null values.
1
Kingdom of Saudi Arabia
المملكة العربية السعودية
Ministry of Higher Education وزارة التعليم العالى
Jazan University
College of Computer Science & Information
TYPE - Aجــامـعــة جـــــازان
كلية الحاسب اآللى ونظم المعلومات
Technology
Q3. Assume that two instance variables (String name, int ID) are declared in a class
Student. Write a parameterized constructor to initialize the values of these variables.
Answer:
public class Student
{
private String name;
private int Id;
Q4. Write a java statement to declare an array of objects using a defined class Car.
Answer :
public static class Car{
public String name;
public int Id;
public Car(int n, String id){
this.name = n;
this.Id=id
}
}
class Array{
public static void main(String[] args){
Car[] car = new Car[100];
car[0] = new Car("BMW ",12345678);
2
Kingdom of Saudi Arabia
المملكة العربية السعودية
Ministry of Higher Education وزارة التعليم العالى
Jazan University
College of Computer Science & Information
TYPE - Aجــامـعــة جـــــازان
كلية الحاسب اآللى ونظم المعلومات
Technology
car[1] = new Car("BMW ",13122133);
car[2] = new Car("BMW ",35434545);
car[3] = new Car("BMW ",675664456);
System.out.println(car[3].Id);
}
}
SECTION – B (2 x 6 = 12 Marks)
Answer ALL Questions. Each Question carries SIX Marks
Q5. Assume an array arr is declared in the main program. Find all values of the variables (first,
last, and mid) in each repetition of the loop in each time calling binarySearch method from the
main:
arr
0 1 2 3 4 5 6
10 20 30 40 50 60 70
public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
System.out.println("Element is found at index: " + mid);
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
3
Kingdom of Saudi Arabia
المملكة العربية السعودية
Ministry of Higher Education وزارة التعليم العالى
Jazan University
College of Computer Science & Information
TYPE - Aجــامـعــة جـــــازان
كلية الحاسب اآللى ونظم المعلومات
Technology
if ( first > last ){
System.out.println("Element is not found!");
Answer:
- binarySearch (arr, 0, arr.length-1, 50);
1 0 10 6 70 3 40
2 4 50 6 70 5 40
3 4 50 4 50 4 50
1 0 10 6 70 3 40
2 0 10 2 30 1 20
1 0 10 6 70 3 40
4
Kingdom of Saudi Arabia
المملكة العربية السعودية
Ministry of Higher Education وزارة التعليم العالى
Jazan University
College of Computer Science & Information
TYPE - Aجــامـعــة جـــــازان
كلية الحاسب اآللى ونظم المعلومات
Technology
Q6. Write lines of code in java using array list commands to do the following:
Answer:
public static void main(String[] args){
// Create an array list of Character and name it Letters .
ArrayList<Character> Letters = new ArrayList<>();
// Add (‘A’, ‘E’, ‘B’, ‘C’, ‘B’, ‘D’) into the list one by one
Letters.add('A');
Letters.add('E');
Letters.add('B');
Letters.add('C');
Letters.add('B');
Letters.add('D');
// Delete ‘C’ from the list
Letters.remove(3);
// Check if the list contains ‘D’ or not.