Vous êtes sur la page 1sur 12

Create Code for Applications

School of Life & Physical Sciences

Associate Degree

COSC 2395: Programming 1


Java

Examination Paper

Semester 2 2009

5 November, 2009

Student Name _________________________________________________

Student Number_____________________ Signature__________________

• This exam consists of 11 pages


• You have 2 hours including 10 min of reading time
• You must score 50% to pass
• No materials are permitted in the exam
• The exam consists of 12 questions.
• The total mark for the exam is 50

Written by Dr Alex Bezen

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

1 What is a value of the integer variable a?

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?

public class MyProgram {


public static void main(String[] args)
{
int num1 = 20, num2 = 7, num3 = 16, temp;
if (num1 < num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
if (num2 < num3)
{
int temp = num2;
num2 = num3;
num3 = temp;
}

System.out.println(“The numbers are “


+ num1 + “, “ + num2 + “, “ + num3);
}
}

(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”;

for (int i = 1; i <= 3; i++)


{
for (int j = 1; j <= 3; j++)
{
if (i * j < 4)
output += “ “ + i * j;
else
output += “ “ + i * j;
}
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)

public class StringDemo


{
public static void main(String[] args)
{
String s1 = new String(“Kate”);
String s2 = new String(“Ken”);
String s3 = s1;
String s4 = s3;
String s5 = “Tom”;
String s6 = “Ken”;
String s7 = new String(“Ken”);
String s8 = “Tom”;
String s9 = new String(“kate”);

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”);
}

System.out.println(“The length of string s6 “ +


s6.length());
char c = s5.charAt(2);
System.out.println(“The character in the second position
of String s5 “ + c);
}
}

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;

public class TestArray {


/** Main method */
public static void main(String[] args) {
final int TOTAL_NUMBERS = 4;
int[] numbers = new int[TOTAL_NUMBERS];

// Read all numbers


for (int I = 0; I < numbers.length; i++) {
String numString = JoptionPane.showInputDialog(
“Enter a number:”);

numbers[i] = Integer.parseInt(numString);
}

int max = numbers[0];


for (int I = 1; I < numbers.length; i++) {
if (max < numbers[i])
max = numbers[i];
}

int count = 0;
for (int I = 0; I < numbers.length; i++) {
if (numbers[i] == max) count++;
}

String output = “The array is “;


for (int I = 0; I < numbers.length; i++) {
output += numbers[i] + “ “;
}

output += “\nThe largest number is “ + max;


output += “\nThe occurrence count of the largest number “
+ “is “ + count;

// Display the result


JoptionPane.showMessageDialog(null, output);
}
}

(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.*;

public class TestStringTokenizer


{
public static void main(String[] args)
{
String s = “John Smith BHP, Coles|Myer”;

StringTokenizer st1 = new StringTokenizer( s, “ ,|”,


true );

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);
}
}

class WoodenBox extends Box1 {


int life = 2;

void info() {
System.out.println(“The number of wooden boxes = “ + life);
}
}

class SteelBox extends Box1 {


int wg = 45;

void info() {
System.out.println(“The price of a steel box is $” + wg);
}
}

class LargeWoodenBox extends WoodenBox {


void info() {
System.out.println(“This is a Huge Wodden box”);
}
}

public class BoxDemo {


public static void main ( String arg[ ] ) {
Box1 b[]=new Box1[5];
b[1]=new Box1();
b[2]=new WoodenBox();
b[3]=new SteelBox();
b[4]=new LargeWoodenBox();

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

Attributes: String subject1, subject2;


int score1, score2;
Methods: A constructor that sets up all attributes
A getSubject1 method that returns a value of the subject1
A getAverageScore method that returns an average score.
A getMaximumScore method that returns a largest score

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

Vous aimerez peut-être aussi