KEMBAR78
Compre PartB | PDF | Anonymous Function | Method (Computer Programming)
0% found this document useful (0 votes)
24 views5 pages

Compre PartB

oops

Uploaded by

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

Compre PartB

oops

Uploaded by

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

Birla Institute of Technology and Science, Pilani, Hyderabad Campus

Date: 16th May, 2024


CS F213 Comprehensive Examination (Part B)
Max. Duration (Parts A
Object oriented Programming Max. Marks (Part B): 60
and B): 180 minutes

Part B

• This is Part B of the examination. Answer all questions. This is a closed


book exam.
• Calculators and any other electronic and communication devices are not allowed in this
examination.
• This question booklet for Part B comprises 5 printed pages and 5 questions,
totaling to 60 marks.
• Answers to all questions in this part must be written in the answer booklet
supplied separately. Please answer in order and answer all parts of a question together.
Answers to questions must be concise and to the point.
• For questions where you are asked to modify the given code in the question,
you don’t need to copy in your answers the code from the question that is
already given and that does not need to be modified. Your answer can say:
“Modify line x:...”, “Insert between lines a and b:...”, etc. Ensure that the
meaning and intent of your answer is clear. A non-exhaustive list of useful
methods is provided in the Appendix at the end of this booklet.
• It is expected that you will follow Object Oriented Principles to design your
programs and follow the naming conventions discussed in the lectures and
labs.
• Only answers written in ink (not in pencil) will be evaluated.
• Write your name and ID number on the answer booklet. Please make sure
that these are legible.
• At the end of the examination, hand over the Part A OMR sheet and the answer booklet
for Part B to the invigilator(s). You may take away the question papers with you.

1. (a) (6 marks) Consider the following class which returns a Print Queue. Another class using the class Print-
Queue may submit a document for printing through an instance of PrintQueue. Consider the system has only
one printer. What is the most significant problem with this code? Modify the code to make it thread-safe,
efficient and overcome the problem identified by you. You don’t have to do anything for line 4.
1 public c l a s s PrintQueue {
2 private s t a t i c PrintQueue pq ;
3 PrintQueue ( ) {
4 // Get t h e p r i n t queue from t h e system
5 }
6
7 public s t a t i c PrintQueue g e t I n s t a n c e ( ) {
8 pq = new PrintQueue ( ) ;
9 return pq ;
10 }
11 }

(b) (3 marks) Can the FocusListener interface for Swing JTextFields be converted into a Lambda expression /
block? With an example explain why or why not.

Page 1 of 5
CS F213. Object Oriented Programming. 16th May, 2024

(c) (3 marks) Explain the use of the annotations @GetMapping and @RequestParam in a Spring Boot appli-
cation.
2. (a) (5 marks) Write a Block Lambda which can take any number of positive integers and return the maximum
value among them. You should include minimal sanity checks, and throw the pre-defined IllegalArgumentEx-
ception when the arguments are invalid. The signature of the functional interface to use for the Block Lambda
is given next:
1 interface MaxInterface {
2 int getMax ( int . . . n ) ;
3 }

(b) (7 marks) Consider the code below. Convert the code, preserving the behaviour, such that functional
interface Runnable is provided the implementation of the run() method through a Lambda expression /
block. Your code should have only one class. The class should have the main() method and one optional
additional overridden run() method, i.e., the run() method implementation can be inline in the Lambda block
or a separate method. The Thread() constructor in the Appendix might be of help.
1 public c l a s s LambdaThreads {
2 public s t a t i c void main ( S t r i n g [ ] a r g s ) {
3 R r 1 = new R( ) ;
4 R r 2 = new R( ) ;
5 r1 . t . s t a r t ( ) ;
6 r2 . t . s t a r t ( ) ;
7 }
8 }
9
10 c l a s s R implements Runnable {
11 Thread t ;
12 int n ;
13 public R( ) {
14 t = new Thread ( t h i s ) ;
15 }
16 public void run ( ) {
17 try {
18 f o r ( int i = 0 ; i < 1 0 ; i ++) {
19 System . out . p r i n t l n ( n ) ;
20 n++;
21 Thread . s l e e p ( 1 0 0 0 ) ;
22 }
23 }
24 catch ( I n t e r r u p t e d E x c e p t i o n i e ) {
25 i e . printStackTrace () ;
26 }
27 }
28 }

3. (a) (3 marks) In 50 words or less, explain what shortcomings of Java AWT does Swing overcome.
(b) (3 marks) In 50 words or less, explain the process of erasure in the context of Generics in Java.
(c) (6 marks) Consider the code given below. Modify the code such that the text for jlbl is set to Button 1 is
clicked when jb1 is clicked and Button 2 is clicked when jb2 is clicked.
1 import j a v a . awt . FlowLayout ;

Page 2 Page 2 of 5
CS F213. Object Oriented Programming. 16th May, 2024

2 import j a v a x . swing . JButton ;


3 import j a v a x . swing . JFrame ;
4 import j a v a x . swing . JLabel ;
5 import j a v a x . swing . S w i n g U t i l i t i e s ;
6
7 public c l a s s ButtonAndLabel {
8 ButtonAndLabel ( ) {
9 JFrame j f r m = new JFrame ( ” Button and L a b e l s ” ) ;
10 j f r m . s e t L a y o u t (new FlowLayout ( FlowLayout .CENTER) ) ;
11 j f r m . s e t D e f a u l t C l o s e O p e r a t i o n ( JFrame . EXIT ON CLOSE) ;
12 jfrm . s e t S i z e (400 ,200) ;
13
14 JButton j b 1 = new JButton ( ” Button 1 ” ) ;
15 JButton j b 2 = new JButton ( ” Button 2 ” ) ;
16 JLabel j l b l = new JLabel ( ” Waiting ” ) ;
17
18 j f r m . add ( j b 1 ) ;
19 j f r m . add ( j b 2 ) ;
20 j f r m . add ( j l b l ) ;
21 j f r m . s e t V i s i b l e ( true ) ;
22 }
23
24 public s t a t i c void main ( S t r i n g [ ] a r g s ) {
25 S w i n g U t i l i t i e s . i n v o k e L a t e r ( ( ) −> new ButtonAndLabel ( ) ) ;
26 }
27 }

4. (a) (3 marks) In 50 words or less, explain the Delegation Event Model in the context of Java Swing.
(b) (6 marks) Consider the Java program below:
1 public r e c o r d StudentRecord ( S t r i n g name , S t r i n g department , double cgpa )
2 {}
3
4 c l a s s StudentRecordHolder {
5 public s t a t i c void main ( S t r i n g [ ] a r g s ) {
6 StudentRecord [ ] s r = new StudentRecord [ 5 ] ;
7
8 // A s s i g n v a l u e s t o s r [ 0 ] . . . s r [ 4 ] h e r e
9
10 // P r i n t i n t h e r e q u i r e d f o r m a t h e r e
11
12 }
13 }
Add lines of code in line 8 to assign create StudentRecord instances in the sr array from Table 1. Add lines of
code in line 10 to display the contents of the record array in the format name, department, cgpa, one record
per line. You are not allowed to create any additional methods, classes or records.

Page 3 Page 3 of 5
CS F213. Object Oriented Programming. 16th May, 2024

Name Department CGPA


Kwame CSIS 7.5
Wheeler EEE 7.0
Linka Mechanical 8.5
Gi Civil 8.9
Ma-Ti Biology 9

Table 1: Table for Question 4(b)

(c) (3 marks) Write a Java Class StudentResult with a StudentRecord instance (as defined in 4(b) above)
composited in the StudentResult class, and another boolean instance variable pass. The
StudentResult(StudentRecord r) constructor takes a StudentRecord instance as a parameter and sets the pass
instance variable to true if the cgpa of r is above 5, and false otherwise.

5. (a) (6 marks) Consider the code below. Add implementations to the methods getAverage() and isSameAver-
age(...) such that line 23 outputs true. The signature of isSameAverage(...) needs to be specified.
1 public c l a s s GenOps<T extends Number> {
2 T [ ] ob ;
3
4 GenOps (T [ ] o ) {
5 t h i s . ob = o ;
6 }
7
8 double g e t A v e r a g e ( ) {
9 // Compute t h e a v e r a g e o f t h e e l e m e n t s o f ob
10 }
11
12 boolean isSameAverage ( . . . ) {
13 // Return t r u e i f t h i s . g e t a v e r a g e ( ) i s n u m e r i c a l l y t h e same as ob2 .
getaverage ()
14 }
15 }
16
17 c l a s s GenMain{
18 public s t a t i c void main ( S t r i n g [ ] a r g s ) {
19 Double [ ] d o u b l e a r r a y = { 1 . 0 , 2 . 0 , 3 . 0 } ;
20 GenOps<Double> s = new GenOps<>(d o u b l e a r r a y ) ;
21 I n t e g e r [ ] i n t a r r a y = {1 , 2 , 3};
22 GenOps<I n t e g e r > i = new GenOps<>( i n t a r r a y ) ;
23 System . out . p r i n t l n ( s . isSameAverage ( i ) ) ;
24 }
25 }

(b) (6 marks) Draw a UML class diagram befitting the following scenario:
• A Person can have a BankAccount
• A Person has a name (String), age (int), gender(Enumeration of Male, Female or Other)
• A Person can be a Student or an Employee
• A Student can be enrolled in a Course (String)
• An Employee can be attached to a Department (String)
• A BankAccount has an accountnumber (long), a balance (double)

Page 4 Page 4 of 5
CS F213. Object Oriented Programming. 16th May, 2024

• A BankAccount can be a SavingsAccount or CurrentAccount

Appendix
The following non-exhaustive list of methods / constructors might be useful:
1. class Thread: Thread(Runnable target): (constructor) Allocates a new Thread object.
2. class JButton: public void addActionListener(ActionListener l): Adds an ActionListener to the button.
3. interface ActionListener: void actionPerformed(ActionEvent e): Invoked when an action occurs.
4. public void setText(String text): Defines the single line of text this component will display. If the value of text is
null or empty string, nothing is displayed. The default value of this property is null.

Page 5 Page 5 of 5

You might also like