SlideShare a Scribd company logo
1 of 7
Download to read offline
1
CIS 7124 Object-Oriented Programming
Sample Midterm Exam
1. Briefly answer each of the following questions.
(a) What is an abstract data type? What is the major difference between an abstract
data type and a type in procedural programming languages?
(b) What is the meaning of the keyword static in Java? What are the differences
between static and non-static members.
( c) What is polymorphism? Give an example to illustrate the use of polymorphic variables in Java.
2
2. Program comprehension
2.1 Find errors (either compile or run-time) in the following Java code snippets if there
are any. State the type of the error and state explicitly “no” if no error exists.
(a)
(b)
(c )
class A {
public A(int x){}
public void m(){
A a = new A();
}
}
class B{
public void main(String[] args){
System.out.println(abs(-2));
}
static int abs(int x){
return x<0 ? -x : x;
}
}
import java.util.*;
class C {
public Stack m(int x){
Stack s = new Stack();
s.push((Integer)x);
return s;
}
}
3
(d)
(e)
(f)
(g)
interface A {
void m(){
System.out.println(“m”);
}
}
class A{
static int x;
public A(int x){
this.x = x;
}
}
class A {}
class B extends A {}
class C extends B {}
class D {
public static void main(String[] args){
A a = new A();
C c = new C();
B b = c;
}
}
class A {
public A(){}
public A(A a){}
}
4
2.2 What are the outputs to the console (standard output) from each of the following
programs?
(a)
(b)
class Test {
int var;
Test(double var){
this.var = (int)var;
}
Test(int var){
this(“aaa”);
}
Test(String s){
this();
System.out.println(s);
}
Test(){
System.out.println(“bbb”);
}
public static void main(String[] args){
Test t = new Test(5);
}
}
class Test {
int x;
public Test(int x){
this.x = x;
}
public static void m(Test o){
o = new Test(2);
}
public static void main(String[] args){
Test o = new Test(1);
m(o);
System.out.println(“output=”+o.x);
}
}
5
(c)
(d)
(e)
class Test {
public static void main(String[] args){
System.out.println(“output=”+m((int)1.5));
}
static int m(int x){
return ++x;
}
}
class Test {
private static int x;
public Test(){
x++;
}
public static void main(String[] args){
Test o1 = new Test();
Test o2 = new Test();
System.out.println(“output=”+x);
}
}
class J extends SuperJ {
public J(){
System.out.println("ConsJ");
}
public void m(){
System.out.println("J");
}
public static void main(String[] args){
SuperJ o = new J();
o.m();
}
}
class SuperJ {
public SuperJ(){
System.out.println("ConsSuperJ");
}
public void m(){
System.out.println("SuperJ");
}
}
6
3. Programming
3.1 Designing an electronic voting machine is a challenging task. You are asked to
implement a very simplified voting machine class with the following specification. A
voting machine has a list of candidates and the following methods:
a) addCandidate(String name)
/* Add a candidate with the name to the list */
b) castVote(String name)
/* Cast a vote to the candidate with the name */
c) printResults()
/* Print out the number of votes each candidate has received. The order does
not matter */
Assume that only two attributes of a candidate, namely, the name and number of votes,
are of interest here, and the size of the candidate list is unknown in advance.
7
3.2 Implement the following functions (static methods) on linked lists of Integer objects :
a) public static boolean sorted(LinkedList<Integer> lst)
/* Return true if the list is sorted in ascending order; otherwise, false. */
b) public static int sum(LinkedList<Integer> lst)
/* Return the sum of the elements in the list. */
c) public static LinkedList<Integer>
replace(LinkedList<Ingeger> lst, Integer o, Integer n)
/* Return a copy of the given list, replacing all occurrences of o with n. Use equals to
test equality*/

More Related Content

Similar to sample_midterm.pdf

MX Server is my friend
MX Server is my friendMX Server is my friend
MX Server is my friendGabriel Daty
 
Execution flow of main() method inside jvm
Execution flow of main() method inside jvmExecution flow of main() method inside jvm
Execution flow of main() method inside jvmsaurav uniyal
 
Simulado java se 7 programmer
Simulado java se 7 programmerSimulado java se 7 programmer
Simulado java se 7 programmerMiguel Vilaca
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab FileKandarp Tiwari
 
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...Udayan Khattry
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphismUsama Malik
 
Multiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritanceMultiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritanceAbishek Purushothaman
 
Java Programs
Java ProgramsJava Programs
Java Programsvvpadhu
 
2 object orientation-copy
2 object orientation-copy2 object orientation-copy
2 object orientation-copyJoel Campos
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iiiNiraj Bharambe
 
Object oriented programming la bmanual jntu
Object oriented programming la bmanual jntuObject oriented programming la bmanual jntu
Object oriented programming la bmanual jntuKhurshid Asghar
 

Similar to sample_midterm.pdf (20)

MX Server is my friend
MX Server is my friendMX Server is my friend
MX Server is my friend
 
Execution flow of main() method inside jvm
Execution flow of main() method inside jvmExecution flow of main() method inside jvm
Execution flow of main() method inside jvm
 
Simulado java se 7 programmer
Simulado java se 7 programmerSimulado java se 7 programmer
Simulado java se 7 programmer
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Core java
Core javaCore java
Core java
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab File
 
JAVAPGMS.docx
JAVAPGMS.docxJAVAPGMS.docx
JAVAPGMS.docx
 
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
 
Core java
Core javaCore java
Core java
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphism
 
Java and j2ee_lab-manual
Java and j2ee_lab-manualJava and j2ee_lab-manual
Java and j2ee_lab-manual
 
Multiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritanceMultiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritance
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Java puzzles
Java puzzlesJava puzzles
Java puzzles
 
2 object orientation-copy
2 object orientation-copy2 object orientation-copy
2 object orientation-copy
 
Java Language fundamental
Java Language fundamentalJava Language fundamental
Java Language fundamental
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iii
 
Object oriented programming la bmanual jntu
Object oriented programming la bmanual jntuObject oriented programming la bmanual jntu
Object oriented programming la bmanual jntu
 
Nabil code
Nabil  codeNabil  code
Nabil code
 

Recently uploaded

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfdanishmna97
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityVictorSzoltysek
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingWSO2
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringWSO2
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceIES VE
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuidePixlogix Infotech
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 

Recently uploaded (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 

sample_midterm.pdf

  • 1. 1 CIS 7124 Object-Oriented Programming Sample Midterm Exam 1. Briefly answer each of the following questions. (a) What is an abstract data type? What is the major difference between an abstract data type and a type in procedural programming languages? (b) What is the meaning of the keyword static in Java? What are the differences between static and non-static members. ( c) What is polymorphism? Give an example to illustrate the use of polymorphic variables in Java.
  • 2. 2 2. Program comprehension 2.1 Find errors (either compile or run-time) in the following Java code snippets if there are any. State the type of the error and state explicitly “no” if no error exists. (a) (b) (c ) class A { public A(int x){} public void m(){ A a = new A(); } } class B{ public void main(String[] args){ System.out.println(abs(-2)); } static int abs(int x){ return x<0 ? -x : x; } } import java.util.*; class C { public Stack m(int x){ Stack s = new Stack(); s.push((Integer)x); return s; } }
  • 3. 3 (d) (e) (f) (g) interface A { void m(){ System.out.println(“m”); } } class A{ static int x; public A(int x){ this.x = x; } } class A {} class B extends A {} class C extends B {} class D { public static void main(String[] args){ A a = new A(); C c = new C(); B b = c; } } class A { public A(){} public A(A a){} }
  • 4. 4 2.2 What are the outputs to the console (standard output) from each of the following programs? (a) (b) class Test { int var; Test(double var){ this.var = (int)var; } Test(int var){ this(“aaa”); } Test(String s){ this(); System.out.println(s); } Test(){ System.out.println(“bbb”); } public static void main(String[] args){ Test t = new Test(5); } } class Test { int x; public Test(int x){ this.x = x; } public static void m(Test o){ o = new Test(2); } public static void main(String[] args){ Test o = new Test(1); m(o); System.out.println(“output=”+o.x); } }
  • 5. 5 (c) (d) (e) class Test { public static void main(String[] args){ System.out.println(“output=”+m((int)1.5)); } static int m(int x){ return ++x; } } class Test { private static int x; public Test(){ x++; } public static void main(String[] args){ Test o1 = new Test(); Test o2 = new Test(); System.out.println(“output=”+x); } } class J extends SuperJ { public J(){ System.out.println("ConsJ"); } public void m(){ System.out.println("J"); } public static void main(String[] args){ SuperJ o = new J(); o.m(); } } class SuperJ { public SuperJ(){ System.out.println("ConsSuperJ"); } public void m(){ System.out.println("SuperJ"); } }
  • 6. 6 3. Programming 3.1 Designing an electronic voting machine is a challenging task. You are asked to implement a very simplified voting machine class with the following specification. A voting machine has a list of candidates and the following methods: a) addCandidate(String name) /* Add a candidate with the name to the list */ b) castVote(String name) /* Cast a vote to the candidate with the name */ c) printResults() /* Print out the number of votes each candidate has received. The order does not matter */ Assume that only two attributes of a candidate, namely, the name and number of votes, are of interest here, and the size of the candidate list is unknown in advance.
  • 7. 7 3.2 Implement the following functions (static methods) on linked lists of Integer objects : a) public static boolean sorted(LinkedList<Integer> lst) /* Return true if the list is sorted in ascending order; otherwise, false. */ b) public static int sum(LinkedList<Integer> lst) /* Return the sum of the elements in the list. */ c) public static LinkedList<Integer> replace(LinkedList<Ingeger> lst, Integer o, Integer n) /* Return a copy of the given list, replacing all occurrences of o with n. Use equals to test equality*/