SlideShare a Scribd company logo
1 of 7
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
Imam University | CCIS
Doc. No. 006-01-20140514
Page 2 of 7
5 7
Total 40
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
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);
}
}
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);
}
}
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); }
}
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;}
}

More Related Content

Similar to Examf cs-cs141-2-17 solution

Cs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solutionCs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solutionFahadaio
 
Cs141 mid termexam2_v1
Cs141 mid termexam2_v1Cs141 mid termexam2_v1
Cs141 mid termexam2_v1Fahadaio
 
Mid1 cs141-1-17-1-final version
Mid1 cs141-1-17-1-final versionMid1 cs141-1-17-1-final version
Mid1 cs141-1-17-1-final versionFahadaio
 
Cs141 mid termexam2_v1answer
Cs141 mid termexam2_v1answerCs141 mid termexam2_v1answer
Cs141 mid termexam2_v1answerFahadaio
 
Inheritance (1)
Inheritance (1)Inheritance (1)
Inheritance (1)sanya6900
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfmayorothenguyenhob69
 
Cs141 mid termexam v5_solution
Cs141 mid termexam v5_solutionCs141 mid termexam v5_solution
Cs141 mid termexam v5_solutionFahadaio
 
BISH CS Modle Exit Exam.doc
BISH CS Modle Exit Exam.docBISH CS Modle Exit Exam.doc
BISH CS Modle Exit Exam.docAnimutGeremew3
 
Programming fundamentals using c++ question paper 2014 tutorialsduniya
Programming fundamentals using c++  question paper 2014   tutorialsduniyaProgramming fundamentals using c++  question paper 2014   tutorialsduniya
Programming fundamentals using c++ question paper 2014 tutorialsduniyaTutorialsDuniya.com
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingRenas Rekany
 
Sample prac exam2013
Sample prac exam2013Sample prac exam2013
Sample prac exam2013hccit
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingSyed Faizan Hassan
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: MethodsSvetlin Nakov
 
Assignment 7
Assignment 7Assignment 7
Assignment 7IIUM
 

Similar to Examf cs-cs141-2-17 solution (20)

Cs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solutionCs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solution
 
Cs141 mid termexam2_v1
Cs141 mid termexam2_v1Cs141 mid termexam2_v1
Cs141 mid termexam2_v1
 
Mid1 cs141-1-17-1-final version
Mid1 cs141-1-17-1-final versionMid1 cs141-1-17-1-final version
Mid1 cs141-1-17-1-final version
 
Cs141 mid termexam2_v1answer
Cs141 mid termexam2_v1answerCs141 mid termexam2_v1answer
Cs141 mid termexam2_v1answer
 
Inheritance (1)
Inheritance (1)Inheritance (1)
Inheritance (1)
 
c++ lab manual
c++ lab manualc++ lab manual
c++ lab manual
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdf
 
Cs141 mid termexam v5_solution
Cs141 mid termexam v5_solutionCs141 mid termexam v5_solution
Cs141 mid termexam v5_solution
 
BISH CS Modle Exit Exam.doc
BISH CS Modle Exit Exam.docBISH CS Modle Exit Exam.doc
BISH CS Modle Exit Exam.doc
 
Programming fundamentals using c++ question paper 2014 tutorialsduniya
Programming fundamentals using c++  question paper 2014   tutorialsduniyaProgramming fundamentals using c++  question paper 2014   tutorialsduniya
Programming fundamentals using c++ question paper 2014 tutorialsduniya
 
E3
E3E3
E3
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Sample prac exam2013
Sample prac exam2013Sample prac exam2013
Sample prac exam2013
 
T1
T1T1
T1
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
CS3391 -OOP -UNIT – II  NOTES FINAL.pdfCS3391 -OOP -UNIT – II  NOTES FINAL.pdf
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
 
Uta005
Uta005Uta005
Uta005
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
Assignment 7
Assignment 7Assignment 7
Assignment 7
 

Recently uploaded

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 

Recently uploaded (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
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;} }