Académique Documents
Professionnel Documents
Culture Documents
Associate Degree
Examination Paper
Semester 2 2009
5 November, 2009
Document: Java_Sem1_2009_Progr1_Exam.doc
Author: Dr Alex Bezen
Save Date: 05/11/2009
RMIT University Page 1 of 12
Create Code for Applications
Document: Java_Sem1_2009_Progr1_Exam.doc
Author: Dr Alex Bezen
Save Date: 05/11/2009
RMIT University Page 2 of 12
Create Code for Applications
a = 45 + 43 % 5 * (23 * 3 % 2);
(1 mark)
2 How would you write the following arithmetic expression in Java?
(1 mark)
3 Assume that x is 1, show the result of the following Boolean expressions:
(2 marks)
4 What will be the output of the following program?
(3 marks)
5 What will be an output of the following program?
Document: Java_Sem1_2009_Progr1_Exam.doc
Author: Dr Alex Bezen
Save Date: 05/11/2009
RMIT University Page 3 of 12
Create Code for Applications
Public class JavaProgram {
/** Main method */
public static void main(String[] args)
{
String output = “ Display\n”;
output += “--------------\n”;
output += “\n”;
System.out.println(output);
}
}
(5 marks)
6 What will be an output of the following program if a user types:
a). ‘A’, b) ‘E’, c) ‘M’, d) ‘K’
public class TestSwitch
{
public static void main(String[] args)
{
char ch;
ch = Console.readChar(“Enter a letter: “);
switch ( ch )
{
case ‘A’:
case ‘B’:
case ‘C’:
System.out.println(“Good”);
break;
case ‘E’:
case ‘F’:
System.out.println(“Not bad”);
case ‘M’:
System.out.println(“OK”);
break;
Document: Java_Sem1_2009_Progr1_Exam.doc
Author: Dr Alex Bezen
Save Date: 05/11/2009
RMIT University Page 4 of 12
Create Code for Applications
default:
System.out.println(“Bad”);
break;
}
}
}
(4 marks)
7 What will be an output of the following program? (12 marks)
if (s1.equals(s2))
{
System.out.println(“s1 equals to s2”);
}
else
{
System.out.println(“s1 not equals to s2”);
}
if (s4.equals(s1))
{
System.out.println(“s4 equals to s1”);
}
else
{
System.out.println(“s4 not equals to s1”);
}
if (s2.equals(s7))
{
System.out.println(“s2 equals s7”);
Document: Java_Sem1_2009_Progr1_Exam.doc
Author: Dr Alex Bezen
Save Date: 05/11/2009
RMIT University Page 5 of 12
Create Code for Applications
}
else
{
System.out.println(“s2 not equals to s7”);
}
if (s1.equals(s6))
{
System.out.println(“s1 equals s6”);
}
else
{
System.out.println(“s1 not equals to s6”);
}
if(s4 == s1)
{
System.out.println(“Reference of s4 equals to
reference of s1”);
}
else
{
System.out.println(“Reference of s4 not equals to
reference of s1”);
}
if(s2 == s7)
{
System.out.println(“Reference of s2 equals to
reference of s7”);
}
else
{
System.out.println(“Reference of s2 not equals to
reference of s7”);
}
if(s8 == s5)
{
System.out.println(“Reference of s8 equals to
reference of s5”);
}
else
{
System.out.println(“Reference of s8 not equals to
reference of s5”);
}
if(s1.equalsIgnoreCase(s9))
{
System.out.println(“s1 equals s9”);
Document: Java_Sem1_2009_Progr1_Exam.doc
Author: Dr Alex Bezen
Save Date: 05/11/2009
RMIT University Page 6 of 12
Create Code for Applications
}
int n = s1.compareTo(s2);
System.out.println(“n = “ + n);
if(n == 0)
{
System.out.println(“s1 equals to s2”);
}
else if(n < 0)
{
System.out.println(“s1 precedes s2”);
}
else if(n > 0)
{
System.out.println(“s2 precedes s1”);
}
8 What will be an output of the following program if a user enters 4 numbers {5,5,4,3}
in the input dialogs?
Document: Java_Sem1_2009_Progr1_Exam.doc
Author: Dr Alex Bezen
Save Date: 05/11/2009
RMIT University Page 7 of 12
Create Code for Applications
Import javax.swing.JoptionPane;
numbers[i] = Integer.parseInt(numString);
}
int count = 0;
for (int I = 0; I < numbers.length; i++) {
if (numbers[i] == max) count++;
}
(6 marks)
9 What will be an output of the following program?
Document: Java_Sem1_2009_Progr1_Exam.doc
Author: Dr Alex Bezen
Save Date: 05/11/2009
RMIT University Page 8 of 12
Create Code for Applications
Import java.util.*;
while ( st1.hasMoreTokens() )
{
System.out.println( st1.nextToken() );
}
}
}
(3 marks)
11 What will be an output of the following program?
Class Box1 {
Document: Java_Sem1_2009_Progr1_Exam.doc
Author: Dr Alex Bezen
Save Date: 05/11/2009
RMIT University Page 9 of 12
Create Code for Applications
int w = 7,h = 5;
void info() {
System.out.println(“This is a simple box”);
System.out.println(“width = “+ w + “ ٛ eight “+ h);
}
}
void info() {
System.out.println(“The number of wooden boxes = “ + life);
}
}
void info() {
System.out.println(“The price of a steel box is $” + wg);
}
}
for(int i=1;i<5;i++) {
b[i].info();
}
}
}
(6 marks)
12 Write a code that implements the following functionality (7 marks)
Class Student
Document: Java_Sem1_2009_Progr1_Exam.doc
Author: Dr Alex Bezen
Save Date: 05/11/2009
RMIT University Page 10 of 12
Create Code for Applications
Document: Java_Sem1_2009_Progr1_Exam.doc
Author: Dr Alex Bezen
Save Date: 05/11/2009
RMIT University Page 11 of 12
Create Code for Applications
Document: Java_Sem1_2009_Progr1_Exam.doc
Author: Dr Alex Bezen
Save Date: 05/11/2009
RMIT University Page 12 of 12