0% found this document useful (0 votes)
133 views

CPS 101: Computer Programming I: General Questions (40 Points)

This document is the midterm exam for a computer programming course. It contains general questions about Java programming concepts like the difference between number and object variables, static variables, and accessor/mutator methods. It also contains a problem set with questions on using the Java Math library, writing a predicate method, and calling methods in another class. There is an optional bonus question to write a method to return the last character of a string. Good luck is wished to the students taking the exam.

Uploaded by

Mike Mecury
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

CPS 101: Computer Programming I: General Questions (40 Points)

This document is the midterm exam for a computer programming course. It contains general questions about Java programming concepts like the difference between number and object variables, static variables, and accessor/mutator methods. It also contains a problem set with questions on using the Java Math library, writing a predicate method, and calling methods in another class. There is an optional bonus question to write a method to return the last character of a string. Good luck is wished to the students taking the exam.

Uploaded by

Mike Mecury
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CPS 101: Computer Programming I

Mid-term Exam
Alfusainey Jallow, Lamin Saidy & Ebrima Jaw April 1st, 2016

Instructions
Please answer all questions. The questions are divided into general and problem set questions! There is
an optional bonus point question worth 10 points.

 Good Luck!

General Questions (40 points )


1. What is the dierence between number variables and object variables. (5 pts )

2. Explain why it is safe to declare the following variable public. (5 pts )

p u b l i c s t a t i c f i n a l LIMIT=2;

3. Explain the dierence between accessor and mutator methods. (5 pts )

4. (5 pts ). What is a static variable in Java? How will the static age variable be called?
1 p u b l i c c l a s s Person {
2 p u b l i c s t a t i c age ;
3 }

5. Is it true that all Java programs must have a main method? Motivate your answer. (5 pts )

6. (5 pts ) What are the rules for declaring a constructor and state it's main function in a Java
program.

7. (10 pts ) Consider the code snippet below. How can you get the string Algebra from the course
variable?
S t r i n g c o u r s e = "Modern Algebra 1 " ;
S t r i n g a l g e b r a = . . . ; // c o m p l e t e t h i s p a r t .

Problem Set (50 points )


1. The Java Math library oers Java programmers useful methods to perform various mathematical
functions. Using the Math library, write a Java method to compute and return the value of the
following formula. (20 pts )
n(n−1) 2
x= 2 (b − 4ac)

Hint: Use the method structure provided below. Feel free to declare additional local variables if
you want.

1 p u b l i c d o u b l e compute ( ) {
2
3 d o u b l e x = u s e t h e Math l i b r a r y h e r e ;
4 ...
5
6 return x ;
7 }

1
2. (15 pts ) Write a predicate method isPositive(int value) that returns true if its explicit param-
eter is postive and false otherwise.

3. (15 pts ). Consider the following Java program


1 p u b l i c c l a s s Person {
2 p r i v a t e S t r i n g name ;
3 p r i v a t e i n t age ;
4 p u b l i c v o i d setName ( S t r i n g name ) {
5 t h i s . name = name ;
6 }
7 p u b l i c v o i d setAge ( i n t age ) {
8 t h i s . age = age ;
9 }
10 }

1 public c l a s s PersonTester {
2 p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
3 // HINT : c a l l t h e setName ( S t r i n g ) and s e t A g e ( i n t ) methods
4 // o f t h e p e r s o n c l a s s h e r e . . .
5 }
6 }

How can you call the setName(String) and setAge(int) methods inside of the main method of the
PersonTester class?

Bonus Point (10 points )


1. Write a method that returns the last character in its argument.
Hint: Use the method structure provide below as starting point.
1 p u b l i c c h a r getLastChar ( S t r i n g s t r ) {
2 char lastChar = ' ' ;
3
4 // 1 . g e t t h e l a s t c h a r a c t e r o f t h e s t r i n g ,
5 // 2 . a s s i g n i t t o y o u r l a s t C h a r v a r i a b l e d e c l a r e d a b o v e .
6
7 return lastChar ;
8 }
9 s

Department of Computer Science (University of The Gambia)

You might also like