Professional Documents
Culture Documents
ii) Explain what is meant by a boolean expression and where they are used.
[3 Marks]
Question 1 continues overleaf. Page 2 of 10 ECS401 Mid term Test Autumn 2008
Question 1 continues overleaf. Page 3 of 10 ECS401 Mid term Test Autumn 2008
1b) Give the value printed out after each fragment of code in the question sub-parts below is executed. Justify your answers. i)
String z = A; String x = B; x = x + z; z = z + x; System.out.println(z); [2 Marks]
Question 1b continues overleaf. Page 4 of 10 ECS401 Mid term Test Autumn 2008
ii)
int y = 0; int x = 2; if(x>5) {y = 10;} else if (x>=2) {y = 15;} else if (x == 2) {y = 20;} else {y = 25;} System.out.println(y); [2 Marks]
Question 1b continues overleaf. Page 5 of 10 ECS401 Mid term Test Autumn 2008
iii)
String m = A; for(int i = 1; i < 4; i++) { m = m + B; } System.out.println(m); [2 Marks]
Question 1b continues overleaf. Page 6 of 10 ECS401 Mid term Test Autumn 2008
iv)
int p = 0; for(int i = 1; i <= 2; i++) { p = p+1; } for(int j = 1; j <= 4; j++) { p = p+1; } System.out.println(p); [3 Marks]
[Total Marks for Part 1b: 9] [Total Marks for Question 1: 15] Test continues overleaf. Page 7 of 10 ECS401 Mid term Test Autumn 2008
Question 2 [10 marks] Write a Java program that comments on the endangered status of different animals. It should ask the user to type in the name of an animal. Depending on the animal named it should print a message giving its status, chosen from: Extinct, Endangered, Vulnerable or Not in danger. For example, if the user typed in Dodo, it should print the message: The Dodo is unfortunately now extinct. The program should first ask how many animals the user would like to check. They type in a number. The program should then ask them for the name of an animal and immediately give a response. It should do this the requested number of times. An example run of the program might go: How many animals do you want to check? 7 OK. You will need to give me 7 animals. Give me the name of animal 1? Dodo The Dodo is unfortunately now extinct. Give me the name of animal 2? Blue Whale The Blue Whale is endangered and needs protection. Give me the name of animal 3? Passenger Pigeon The Passenger Pigeon is unfortunately now extinct Give me the name of animal 4? Gruffalo The Gruffalo is one I have not heard of. Give me the name of animal 5? Albatross The Albatross is endangered and needs protection. Give me the name of animal 6? Cheetah The Cheetah is vulnerable. Give me the name of animal 7? Rabbit The Rabbit is not in danger. Your program should be able to give the correct response for each of the animals above. Others it can just reply that it has not heard of them as above. The responses given for each category should exactly match those in the example above. Your program may use pop-up windows to ask questions and display its responses.
[10 Marks] Write your answer overleaf.
Page 8 of 10
Answer to question 2:
Page 9 of 10
Page 10 of 10