[go: up one dir, main page]

0% found this document useful (0 votes)
79 views5 pages

Algorithm and Data Structures

The document is an exam for a Programming 2 course, containing 6 multiple choice questions testing knowledge of classes, arrays, and array lists in Java. The questions cover topics like class attributes, features of array lists, parameterized constructors, declaring arrays of objects, finding values in a binary search method, and using array list commands like add, delete, contains, indexOf, and sort.

Uploaded by

abdh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views5 pages

Algorithm and Data Structures

The document is an exam for a Programming 2 course, containing 6 multiple choice questions testing knowledge of classes, arrays, and array lists in Java. The questions cover topics like class attributes, features of array lists, parameterized constructors, declaring arrays of objects, finding values in a binary search method, and using array list commands like add, delete, contains, indexOf, and sort.

Uploaded by

abdh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Kingdom of Saudi Arabia

‫المملكة العربية السعودية‬


Ministry of Higher Education ‫وزارة التعليم العالى‬
Jazan University
College of Computer Science & Information
TYPE - A‫جــامـعــة جـــــازان‬
‫كلية الحاسب اآللى ونظم المعلومات‬
Technology

ACADEMIC YEAR 2019-2020 | SECOND SEMESTER


FINALEXAM (ASSIGNMENT)

Code: COMP - 112


Course: Programming 2 Section:

Student Name: Reham Ali Algadi Student ID: 201913332


Exam Date : 05-05-2020 Start Time:8.00 Signature of
CT:____________________________
Exam Duration:
Max. Marks: 20 Marks awarded:

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.

Q2. List two features of array list.

Answer :
1- Java ArrayList allows duplicate and null values.

2- Java ArrayList is an ordered collection. It maintains the insertion order of the


elements.

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;

public Student (String name, int id)


{
this.name = name;
this.Id=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

- binarySearch (arr, 0, arr.length-1, 50);


- binarySearch (arr, 0, arr.length-1, 20);
- binarySearch (arr, 0, arr.length-1, 40);

Binary search method:

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

No first values Arr[first ] last values Arr[last ] mid values Arr[mid ]

1 0 10 6 70 3 40

2 4 50 6 70 5 40

3 4 50 4 50 4 50

Output =Element is found at index: 4

- binarySearch (arr, 0, arr.length-1, 20);

No first values Arr[first ] last values Arr[last ] mid values Arr[mid ]

1 0 10 6 70 3 40

2 0 10 2 30 1 20

Output =Element is found at index: 1


- binarySearch (arr, 0, arr.length-1, 40);

No first values Arr[first ] last values Arr[last ] mid values Arr[mid ]

1 0 10 6 70 3 40

Output =Element is found at index: 3

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:

- Create an array list of Character and name it “Letters”.


- Add (‘A’, ‘E’, ‘B’, ‘C’, ‘B’, ‘D’) into the list one by one.
- Delete ‘C’ from the list.
- Check if the list contains ‘D’ or not.
- Find the last index of ‘B’.
- Sort the list.

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.

System.out.println("The Letters array contain \'D\'? :" +


Letters.contains('D'));
// Find the last index of ‘B’.
System.out.println("lastIndexOf Letter \'B\': " +Letters.lastIndexOf('B'));
// Sort the list
Collections.sort(Letters);
}

You might also like