Continued j what is the output of the following code

This preview shows page 6 - 11 out of 20 pages.

CONTINUED j) What is the output of the following code? String a = "21"; System.out.println(5 + a); System.out.println(3 + Integer.parseInt(a)); 521 24 (2 marks) k) What is the output of the following code? double a = 21/10; System.out.println(a); 2.0 (2 marks) l) What is the output of the following code? int a = 5; int b = 6; int c = b; c = c + 3; a = b; b = b + 1; System.out.println(a + ", " + b + ", " + c); 6, 7, 9 (2 marks)
Image of page 6

Subscribe to view the full document.

Question/Answer Sheet - Page 7 - CompSci 101 S1 C SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED m) The following code obtains an int value from the user, adds 5 to the value entered by the user and prints the result. Complete the code. String input; int num; System.out.print("Enter number: "); input = Keyboard.readInput(); num = Integer.parseInt(input); num = num + 5; System.out.println(num); (2 marks) n) What is the output of the following code? int a = 6 % 8; int b = 27 % 9; int c = 24 % 5; System.out.println(a + ", " + b + ", " + c); 6, 0, 4 (2 marks) o) Write a boolean expression which tests if the int variable, value , contains a number which is odd. value % 2 == 1 (2 marks)
Image of page 7
Question/Answer Sheet - Page 8 - CompSci 101 S1 C SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED p) Write a boolean expression which tests if the int variable, value , contains a number which is between 4 and 50 inclusive. value >= 4 && value <= 50 (2 marks) q) What is the output of the following code? String word = "AMAZING"; int pos1 = word.indexOf("A"); int pos2 = word.indexOf("Z"); int pos3 = word.indexOf("n"); System.out.println(pos1 + ", " + pos2 + ", " + pos3); 0, 3, -1 (2 marks) r) What is the output of the following code? String words = " ZIP IT UP "; words = words.trim(); int length = words.length(); System.out.println(length); 9 (2 marks)
Image of page 8

Subscribe to view the full document.

Question/Answer Sheet - Page 9 - CompSci 101 S1 C SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED s) Complete the output of the following code. String word1 = new String("HOP"); String word2 = new String("hop"); String word3 = word2.toUpperCase(); System.out.println("1. " + word1.equals(word2)); System.out.println("2. " + word1.equals(word3)); 1. false 2. true (2 marks) t) What is the output when the following program is executed? public class MyProgram { public void start() { int a = 4; methodOne(a); System.out.println("1: " + a); } private void methodOne(int a) { a = a + 2; System.out.println("2: " + a); } } 2. 6 1. 4 (2 marks)
Image of page 9