Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Examf cs-cs141-2-17 solution
1. Imam University | CCIS
Doc. No. 006-01-20140514
Page 1 of 7
Al Imam Mohammad Ibn Saud Islamic University
College of Computer and Information Sciences
Computer Science Department
Course Title: Computer Programming 2
Course Code: CS141
Course
Instructor:
Dr Ashraf A. Shahin, Dr. Alaa Eldeen Ahmed, Dr.
Qaisar Abbas, Dr. Talal Albalawi, Dr. Sarah Alhassan,
Dr. Mai Al-Ammar, Dr. Dania Alomar, Dr. Mashael
Almedlej, Dr. Shahad Alqefari, Dr. Aram Sedrani
Exam: Final Exam
Semester: Spring 2017
Date: May 14, 2017
Duration: 120 Minutes
Marks: 40
Privileges: ☐ Open Book
☐ Calculator
Permitted
☐ Open Notes
☐ Laptop Permitted
Student Name (in
English):
SolutionStudent ID:
Section No.:
Instructions:
1. Answer 5 questions; there are 5 questions in 8 pages.
2. Write your name on each page of the exam paper.
3. Write your answers directly on the question sheets. Use the ends of the question
pages for rough work or if you need extra space for your answer.
4. If information appears to be missing from a question, make a reasonable
assumption, state your assumption, and proceed.
5. No questions will be answered by the invigilator(s) during the exam period.
Official Use Only
Question Student Marks Question Marks
1 5
2 10
3 10
4 8
2. Imam University | CCIS
Doc. No. 006-01-20140514
Page 2 of 7
5 7
Total 40
3. Imam University | CCIS
Doc. No. 006-01-20140514
Page 3 of 7
Student Name (in
English):
__________________________________________ Student
ID:
_____________________________
Question 1: To be answered in (15) Minutes [ ] / 5 Marks
Select the appropriate option from multiple-choice questions and try fill in the following box
with your choice for each point. (one mark for each point)
1 2 3 4 5
B B D E D
1. Static methods cannot be accessed directly without creating the objects of the class in
which they are declared?
A. True.
B. False.
C. Somewhat.
D. None of these.
2. Which of the following keywords allows a subclass to access a superclass method even
when the subclass has overridden the superclass method_____________?
A. this.
B. super
C. static
D. Both (A) and (B)
E. None of the above.
3. Creating a new class using one or more existing classes is known as……………
A. Polymorphism
B. Encapsulation
C. Overloading
D. Inheritance
E. Generic Classes
4. Ability of a function or method call to take different forms as its signature is known as
____.?
A. Overloading
B. Overriding
C. abstract
D. interface
E. Polymorphism
5. Which of following is a valid class using the given code?
public interface A { public void showA(); }
A. public class B extends A { public void showA(){} }
B. public class B implements A { public abstract void showA(){} }
C. public class B implements A { void showA(){} }
D. public class B implements A { public void showA(){} }
E. None of these
4. Imam University | CCIS
Doc. No. 006-01-20140514
Page 4 of 7
Student Name (in
English):
__________________________________________ Student
ID:
_____________________________
Question 2: To be answered in (25) Minutes [ ] / 10 Marks
Fill in the following box with the output(s) of the following code segments
(2.5 marks for each point)
1 2 3 4
6 4 B 9
1) 2)
class Q {
public static int count;
int x=2;
Q( )
{ count+=x; }
Q(int x)
{ count+=x; }
}
public class Q4 {
public static void main (String[] args){
Q a=new Q(4);
Q b=new Q();
Q c;
System.out.print(Q.count);
}
}
public class Q2 {
public static <T > T max( T x ,T y)
{
T max=x;
return max;
}
public static int max( int x , int y)
{
if(x>y) return x+5;
else return y+1;
}
public static void main (String[] argu){
System.out.println( max(2, 3));
}
}
3) 4)
class E2 {
public void print()
{System.out.println("A");}
}
class E3 extends E2{
@Override
public void print()
{System.out.println("B");}
}
class E4 extends E3{
public void show()
{System.out.println("C");}
}
class Q1 {
public static void main (String[] args){
E2 a=new E4();
a.print();
}
}
class E1
{
int x=2;
private int y=2;
public E1()
{ x=1; y=1; }
public E1(int a)
{ x=y=a; }
public E1(int a, int b)
{ this(a+b); }
}
class Q2 {
public static void main(String[] args) {
E1 a=new E1(3,6);
System.out.println(a.x);
}
}
5. Imam University | CCIS
Doc. No. 006-01-20140514
Page 5 of 7
Student Name (in
English):
__________________________________________ Student
ID:
_____________________________
Question 3: To be answered in(25) Minutes [ ] / 10 Marks
In the following code, there are one compilation error in each code segment. List the line number
and the cause of the error.
Question # Line # Cause of the error
1 9
method show cannot be applied to int[], where T is a variable type
extends Object class
2 8 Static method getN() cannot use non-static variable this
3 12 InputMismatchException has to be imported
4 12 cannot assign a value to final variable a
(2.5 marks for each point)
1) 2)
1
2
3
4
5
6
7
8
9
10
11
12
public class Q3 {
public static <T > void show ( T [] a )
{
System.out.print(a[0]);
}
public static void main(String [] args)
{
int [] a={2,4,6,4,9,8};
show(a);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
class Q {
protected static int n;
int m;
Q(int m)
{ this.m=m; }
public static int getN()
{ return this.n ; }
public int getM()
{ return this.m; }
}
3) 4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Q3.java
package finalExam;
import java.util.Scanner;
public class Q3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
try{
int x= sc.nextInt();
int y= sc.nextInt();
System.out.println(x/y);
}catch(InputMismatchException i)
{ System.out.println(i); }
catch(ArithmeticException a)
{ System.err.println(a.toString()); }
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
interface Q4
{
int a=0;
void show();
}
class QSub implements Q4
{
@Override
public void show()
{
a++;
System.out.print(a);
}
}
6. Imam University | CCIS
Doc. No. 006-01-20140514
Page 6 of 7
Student Name (in
English):
__________________________________________ Student
ID:
_____________________________
Question 4: To be answered in(20) Minutes [ ] / 8 Marks
(a) Write a single generic method for the following methods (2 points).
public static void print( int x , int n)
{ for (int i=0;i<n;i++) System.out.println(x); }
public static void print( double y , int a)
{ for (int i=0;i<a;i++) System.out.println(y); }
public static void print( String z , int b)
{ for (int i=0;i<b;i++) System.out.println(z); }
.......................................................................................................................................................
(b) Write a generic method CountGreaterthan that receives an array from any type and an
element e from the same type and counts the array elements that are greater than e (3
points).
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
(c) Declare new exception type called NewException that extends Exception with message
“NewException” (3 points).
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
public static <T> void print( T x , int n) // 1 mark
{ for (int i=0;i<n;i++) System.out.println(x); } // 1 mark
public static <T extends Comparable<T>> int CountGreaterthan (T[] a, T e) // 1 mark
{
int count=0; // 0.5 mark
for(T c:a) // 0.5 mark
if(c.compareTo(e)>0) count++; // 0.5 mark
return count; // 0.5 mark
}
public class NewException extends Exception { // 0.5 mark
public NewException() // 1 mark
{ super("NewException"); }
public NewException(String message) // 0.5 mark
{ super(message); }
public NewException(String message,Throwable throwable ) // 0.5 mark
{ super(message,throwable); }
public NewException(Throwable throwable ) // 0.5 mark
{ super(throwable); }
}
7. Imam University | CCIS
Doc. No. 006-01-20140514
Page 7 of 7
Student Name (in
English):
__________________________________________ Student
ID:
_____________________________
Question 5: To be answered in(25) Minutes [ ] / 7 Marks
Write java code for the following:
- Interface called VehicleInterface with one method calcuatePrice that will calculate and
return price.
- Write a Truck class with parameterized constructer. Truck class implements the interface
VehicleInterface with one data member called noOfWheels (no of wheels). Price of each
truck is calculated as following: (noOfWheels *1000.)
- Write a Car class with parameterized constructer. Car class implements the interface
VehicleInterface with one data member called motorCapacity (capacity of the motor).
Price of each car is calculated as following: ( motorCapacity *100.)
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
......................................................................................................................................................
interface VehicleInterface // 1 mark
{ public double calcuatePrice(); // 1 mark
}
class Truck implements VehicleInterface // 0.5 mark
{
public int noOfWheels; // 0.5 mark
Truck(int noOfWheels) // 0.5 mark
{this.noOfWheels=noOfWheels;}
@Override // 0.5 mark
public double calcuatePrice() // 0.5 mark
{ return noOfWheels*1000;}
}
class Car implements VehicleInterface // 0.5 mark
{
public int motorCapacity; // 0.5 mark
Car(int motorCapacity) // 0.5 mark
{this.motorCapacity=motorCapacity;}
@Override // 0.5 mark
public double calcuatePrice() // 0.5 mark
{ return motorCapacity*100;}
}