Skip to content
March 4, 2015 / TDS Software

Curtin 2014 PDM 300 Mid Sample Question paper and student answers

Question papaer:

paper2014

Answers:

New Doc 12

Advertisements
October 7, 2013 / TDS Software

Write the output of the following Java program. (2 marks)

package javaisfun;

public class JavaIsFun {

public static void main(String[] args) {
String s1 = “Java is Fun!”;
String s2 = new String (“Java is Fun!”);
StringBuffer sb1 = new StringBuffer(“Java is Fun!”);

System.out.println(s1.substring(8)==s1.substring(8,11));
System.out.println(Character.isUpperCase(s1.toUpperCase().charAt(4)));
System.out.println(sb1.delete(3,6).charAt(3)!= s2.charAt(3));
System.out.println(sb1.reverse().append(“Java”).lastIndexOf(“Java”));
}
}

 

false
false
true
9

October 7, 2013 / TDS Software

Java Q&A

Write a Java program to store the following data set in a two dimensional array. Then access
the aray and write all these information in to a text file called “student_data.txt” . Use a
traditional for loop to access the array. Save your answer as “Question13.java”
STUDENT NO STII SPD PS DCCNII SEI
DIT/10/C1/0001 A A- B+ A ADIT/
10/M2/0123 C- C B B B+
DIT/10/M1/0054 D C- C B- B
DIT/10/M1/0025 A A A- A ADIT/
10/C2/1254 C C- B B+ B

 

Re-write the above Java program again. Use a for-each loop to access the array. Save your
answer as “Question14.java”

 

import java.io.*;
class Question14{
public static void main(String[] args) {
String s[][]={{“STUDENT NO”, “STII”, “SPD”, “PS”, “DCCNII”, “SEI”},
{“DIT/10/C1/0001”, “A”, “A-“, “B+”, “A”, “A”},
{“DIT/10/M2/0123”, “C-” ,”C” ,”B” ,”B”, “B+”},
{“DIT/10/M1/0054”, “D” ,”C-” ,”C” ,”B-“, “B”},
{“DIT/10/M1/0025”, “A” ,”A” ,”A-“, “A”, “A”},
{“DIT/10/C2/1254”, “C” ,”C-” ,”B” ,”B+”, “B”}};

try {
File file = new File(“E:/tudent_data.txt”);

// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);

for(String[] sI : s){
String content = “”;
for (String sJ : sI){
content += sJ + “\t” ;
}

bw.write(content);
bw.newLine();
}

bw.close();
System.out.println(“Done”);

} catch (IOException e) {
e.printStackTrace();
}
}
}

October 7, 2013 / TDS Software

Java Q&A

Write a Java program to store the following data set in a two dimensional array. Then access
the aray and write all these information in to a text file called “student_data.txt” . Use a
traditional for loop to access the array. Save your answer as “Question13.java”
STUDENT NO STII SPD PS DCCNII SEI
DIT/10/C1/0001 A A- B+ A ADIT/
10/M2/0123 C- C B B B+
DIT/10/M1/0054 D C- C B- B
DIT/10/M1/0025 A A A- A ADIT/
10/C2/1254 C C- B B+ B

 

import java.io.*;
class Question13{
public static void main(String[] args) {
String s[][]={{“STUDENT NO”, “STII”, “SPD”, “PS”, “DCCNII”, “SEI”},
{“DIT/10/C1/0001”, “A”, “A-“, “B+”, “A”, “A”},
{“DIT/10/M2/0123”, “C-” ,”C” ,”B” ,”B”, “B+”},
{“DIT/10/M1/0054”, “D” ,”C-” ,”C” ,”B-“, “B”},
{“DIT/10/M1/0025”, “A” ,”A” ,”A-“, “A”, “A”},
{“DIT/10/C2/1254”, “C” ,”C-” ,”B” ,”B+”, “B”}};

try {
File file = new File(“E:/tudent_data.txt”);

// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);

for(int i = 0; i<s.length; i++ ){
String content = “”;
for (int j = 0; j< s[i].length; j++){
content += s[i][j] + “\t” ;
}

bw.write(content);
bw.newLine();
}

 

bw.close();

System.out.println(“Done”);

} catch (IOException e) {
e.printStackTrace();
}
}

 

}

October 7, 2013 / TDS Software

Java Q&A

Write a Java program to read a sentence which contains more than 10 words as a keyboard
input and to find the number of words in the sentence that starts with letter ‘i’. You must
use class StringTokenizer to break the sentence into words. Save your answer as
“Question12.java”

 

 

 

import java.io.*;
import java.util.StringTokenizer;
class Question12{
public static void main(String[] args) {
String inp =””;
try{
BufferedReader bf = new BufferedReader (new InputStreamReader (System.in));
System.out.print(“Enter a Sentence : “);
inp = bf.readLine();

}catch (IOException e){
e.printStackTrace();
}

StringTokenizer st2 = new StringTokenizer(inp, ” “);
int count = 0;
while (st2.hasMoreElements()) {
String s = st2.nextElement().toString();
if (s.charAt(0)==’i’){
System.out.println(s);
count++;
}
}
System.out.println(“Number of words starting with ‘i’ : ” + count );
}
}

October 7, 2013 / TDS Software

Java Q&A

Write a Java program to perform a mathematical calculation between two numbers entered
by the user. Whenever you run the program main method should display an output (main
menu) as given below.
Get the mathematical operation to be performed from the user as a keyboard input.
Once the mathematical operation is selected get the two number as keyboard inputs.
Display the result of operation and display the main menu again.
You must perform calculation between numbers until user decided to exit from the
program.
Save your answer as “Question11.java”.

 

Enter
* : For multiplication
/ : For division
+ : For addition
– : For Subtraction
Any other character : To exit
Mathematical Operation : _

 

import java.io.*;

class Question11{
public static void main(String[] args) {
while(true){
System.out.println(“Enter”);
System.out.println(“* : For multiplication”);
System.out.println(“/ : For division”);
System.out.println(“+ : For addition”);
System.out.println(“- : For Subtraction”);
System.out.println(“Any other character : To exit”);

try{
BufferedReader bf = new BufferedReader (new InputStreamReader (System.in));
System.out.print(“Mathematical Operation : “);
String inp =””;
inp = bf.readLine();

Double n1, n2;
System.out.print(“Enter a number : “);
n1 =Double.parseDouble(bf.readLine());

System.out.print(“Enter another number : “);
n2 =Double.parseDouble(bf.readLine());

char oprt = inp.charAt(0);
System.out.print( “Answer : ” );
if(oprt==’*’){
System.out.print( n1 * n2 );
}else if(oprt == ‘/’){
System.out.print( n1 / n2 );
}else if(oprt == ‘+’){
System.out.print( n1 + n2 );
}else if(oprt == ‘-‘){
System.out.print( n1 – n2 );
}else{
System.exit(1);
}
System.out.println(“\n__________________________”);
}catch (IOException e){
e.printStackTrace();
}

}
}
}

October 7, 2013 / TDS Software

Java Q&A

Write a java program to get a string as a key board input. Then count the number of letters,
digits and spaces available in that string and display the count of letters, digits and spaces.
Save your answer as “Question10.java”.

 

import java.io.*;

class Question10{
public static void main(String[] args) {
String inp =””;
try{
BufferedReader bf = new BufferedReader (new InputStreamReader (System.in));
System.out.print(“Enter a String : “);
inp = bf.readLine();

}catch (IOException e){
e.printStackTrace();
}

int letrs = 0 , digits = 0, spaces = 0;
for (int i = 0 ; i < inp.length(); i++){
char a = inp.charAt(i);
if (Character.isSpaceChar(a)){
spaces++;
}else if(Character.isDigit(a)){
digits++;
}else if (Character.isLetter(a)){
letrs++;
}
}

System.out.println(“Number of Letters : ” + letrs);
System.out.println(“Number of Digits : ” + digits);
System.out.println(“Number of Spaces : ” + spaces);
}
}