CAT I Model Answers
CAT I Model Answers
Instructions:
● Answer all the questions.
● Use comments on your code where applicable.
● You are recommended to use java programming language for all coding
problems. If you choose a different OO language, mention the used language
with every problem.
● This paper contains nine (9) pages.
● Time allocation is 75 minutes.
● This is a closed book examination.
● Number and attach any extra sheets and draft sheets used
Wrong answers : Integer, String, char, array, unsigned, bit, bool, boolean
1.2 Briefly explain the role of a compiler and describe how Java Compiler(s) are different
from other compilers. 5 marks
Compiler is a computer program that converts/translates code written in a high level
programming language to a machine understandable form. 3 marks
While other compilers convert the code into machine language java compilers convert
it to an intermediate form known as the byte code. 2 marks
1.3 Identify and circle the errors in the programs given below. A program may contain more
than one error. Write the identified errors with an explanation on the right side of the
paper.
If only 1 error is identified 1 mark
If some are identified 1 mark
If all are identified 1 mark
If non-errors are identified as errors -1 mark
9 marks
class Driver{ |
public static void main(String[] args)[ | For should be for
For{int i = 0; i =< 10; i++}( |
system.out.println(“i”) | system.out.println should be
) | System.out
] |
} | semicolon ( ; ) - missing in line
4
|
|
class Phone{ |
string brand; | string should be String
string model; | string should be String
long IMEI; |
String os; |
| boid should be void
boid public(Boolean stateOn){ | boid public should be
public void
if(stateOn) powerOn; | powerOn();
else powerOff(); |
else restart(); | else without if
} |
} |
These 2 are not errors but marks won’t be reduced if identified : Use of {}, Boolean ->
boolean
class Stars{ |
| Public should be public
Public static void main(){ | main(String[] args){
for(k--; int k = 50; k > 0){ | int k = 50; k > 0; k--;
System.out.println(“ * ”); | is the correct order
if(k % 7 = 0){ | if(k % 7 == 0) // == operator
System.out.println(“”); | this line is correct
} |
} |
} |
} |
|
1.4 Complete the program given below so that it prints out the following pattern to the default
console using suitable data structures and a maximum of two System.out.print() /
System.out.println() statements. It is recommended to use only just one. 10 marks
1
1 3
1 3 6
1 3 6 10
1 3 6 10 15
1 3 6 10 15 21
class Pattern{
public static void main(String[] args){
//proper java syntax 2 marks
// using loops (while, do while, for) 2 marks
//using nesting. Loop inside Loop 2 marks
//OR if inside loop
//using an assignment like x += y 2 marks
// if i++ is used.(although not appropriate) 1 mark
Single Selection/ if
Double Selection/ if else 1 mark
Multiple Selection/ switch, if else if ladder
Iteration 3 marks
while
do while 1 mark
for
Missing marks are compensated for if explained using flowcharts.
2.2. Explain the behaviour of a do while loop with an example. 10 marks
The code in the do while loop is executed once before checking the condition and then
keeps running until the condition becomes false.
int x = 5; explanation : 5 marks
2. 3. Write a program that will calculate the interest given the account type and the account
balance. The interest calculation will happen as follows, 10 marks
}else{