Nothing needs to be fixed, the program will run and print Hello World!
Nothing needs to be fixed, the program will run and print first Hello World! and then And Hello universe!
Nothing needs to be fixed, the program will run and print first And Hello universe! and then Hello World!
Remove the And Hello universe! print statement
Move the And Hello universe! print statement into the main method
The main method needs to be removed
Solution for Question 4
The first print statement (“And Hello universe!”) needs to be either removed or moved into the main method.
Because in Java, any statement of code to be executed needs to be placed inside a method!
(Lesson 1)
Question 5
Given the following program, what will it do when we run it?
classMain{publicstaticvoidmainFirst(String[]args){System.out.println("Hello world!");}publicstaticvoidmain(String[]args){System.out.println("Hello other world!");}}
It will print nothing
The program will not run as it fails with an compile error
It will print Hello other world!
It will print Hello world!
Solution for Question 5
The program will only print Hello other world!.
This is because a Java program always starts by executing the main method.
It cannot print Hello world! as this print statement is inside the mainFirst method but mainFirst is never called from the main method.
(Lesson 1)
Question 6
Given the following program, what will it do when we run it?
classMain{publicstaticvoidmain(String[]args){System.out.println("Hello world!");}publicstaticvoidmain(String[]args){System.out.println("Hello other world!");}}
It will print nothing
The program will not run as it fails with an compile error
It will print Hello other world!
It will print Hello world!
Solution for Question 6
The program will not compile: we have defined our main method with same parameters twice in the class which is not allowed in Java
(Lesson 1)
Question 7
Given the following program, what will it do when we run it?
classMain{publicstaticvoidmain1(String[]args){System.out.println("Hello world!");}publicstaticvoidmain2(String[]args){System.out.println("Hello other world!");}}
It will run and print nothing
It will print Hello other world!
It will print Hello world!
The program will not run as it fails with an error
Solution for Question 7
The program can be compiled successfully.
But when we then try to run it, it will fail with an error.
This is due to the fact that we do not have a public static void main(String[] args) method defined in our program which is needed in any Java program as the entrypoint!
(Lesson 1)
Question 8
Given the following program, what will it do when we run it?
classFirst{publicstaticvoidmain(String[]args){System.out.println("Hello world!");}}classSecond{publicstaticvoidmain(String[]args){System.out.println("Hello other world!");}}
It will always print Hello other world!
It will always print Hello world!
It will print only Hello other world! if we try to run it as “java Second”
It will print only Hello world! if we try to run it as “java First”
The program will not compile if the Java file name is Main.java
Solution for Question 8
What the program does, depends firstly on the name of the source code file:
If the name is First.java it will compile
If the name is Second.java it will compile
If the name is anything else it will not compile
Remember: a Java source code file needs to contain a class with the same name as the file
What the compiled program does depends on how we run it:
if we run it with java First it will execute the main method of the First class and print Hello world!
if we run it with java Second it will execute the main method of the Second class and print Hello other world!
(Lesson 1)
Question 9
Given the following program, what will it do when we run it?
classMain{publicstaticvoidmain(String[]args){System.out.println("Hello world!");Test.test1();Test.test2();}}classTest{publicstaticvoidtest1(){System.out.println("Hello other world 1!");}}classTest{publicstaticvoidtest2(){System.out.println("Hello other world 2!");}}
The program will not compile and fail with an error
It will print Hello world!
It will print Hello other world 1!
It will print Hello other world 2!
Solution for Question 9
The program will not compile as we have two classes with the same name in the same Java source code file!
(Lesson 1)
It will print first Hello and then World in a new line
It will print first Hello and then Everybody in a new line
It will print first Everybody and then Hello in a new line
It will not compile and fail with an error
Solution for Question 10
When the program is ran, it will first print Hello and then in a new line Everybody
These are the values that are assigned to the variables text1 and text2 at the time the print statements are executed
Note, that System.out.println jumps to a new line after printing.
(Lesson 2)
It will print first Hello and then World in a new line
It will print first Hello and then Everybody in a new line
It will print first Everybody and then Hello in a new line
It will not compile and fail with an error
Solution for Question 11
We cannot run the program as it will not compile, i.e. we will get an error.
The reason is the second line of the main method.
We use the variable text2 for the first time but we did declare it by specifying what type it should have.
It should be: String text2 = “World”
(Lesson 2)
It will print first Hello and then World in a new line
It will print first Hello and then Everybody in a new line
It will print first Everybody and then Hello in a new line
It will not compile and fail with an error
Solution for Question 12
We cannot run the program as it will not compile, i.e. we will get an error.
The reason is the third line of the main method.
We declare the variable text1 a second time here though it already exists in our program.
Note, that the declaration means we specify type and variable name!
(Lesson 2)
Question 13
Which of the assignments in the following program are valid?
The only valid assignment is the assignment of i to h:
h is an int and all other types may contain bigger values (i.e. values with more information) than an int can represent.
As Java does not allow an assignment that leads to a loss of information, these are all not allowed and lead to a compile error!
Note: in such assignments where the content of one variable is assigned to another, the actial value of the variable is not of importance - the types of both variables must allow the assignment (that’s why in our example, the assignment of l which is just 1 is also disallowed)
(Lesson 2)
Question 15
Which of the assignments in the following program are valid?
The valid assignments are i or l to h:
h is an long and so decimal values cannot be assigned to it as decimal values may contain more information (i.e. the numbers after the decimal point) than a long can represent
i can be assigned to h as each value of an int can be represented by a long
As Java does not allow an assignment that leads to a loss of information, these are all not allowed and lead to a compile error!
(Lesson 2)
Question 16
Which of the assignments in the following program are valid?
The only invalid assignment is of d to h
h is an float and d a double and a double can contain bigger values (i.e. more information) than a float can represent
As Java does not allow an assignment that leads to a loss of information, this is not allowed and lead to a compile error!
Any long or int can be assigned to a float (the decimal part just becomes .0)
(Lesson 2)
Question 17
Which of the assignments in the following program are valid?
The program will print first 2 => we divide two int thus the result is also an int and for divisions of two int any leftover is omitted.
Then it will print 2.5 => we divide two double thus the result is also a double => thus we have a precise result of the division with the decimal value.
(Lesson 3)
The program will print two times a 2.5
In both cases, we have a division of an int and an double. When applying an operator to two different types, Java will always use for the result the bigger type (i.e. the one that can hold more information). And in this case, it is double (remember that double is the most expressive numerical type in Java)
(Lesson 3)
The program will print first 2.0 and then 2.5.
The 2.5 should be clear: we divide two double and assign the result to a double.
Let’s look what happens for the first result:
first, a division of two int values happen => the result is just 2
then, this result is assigned to a double variable => as this is a decimal number, the 2 is converted to be also a decimal number, i.e. 2.0
(Lesson 3)
Question 21
Given the following program, which of the assignments are incorrect and lead to an error?
The assignment to result1 is valid: division of i1 and i2 results in an int and can be assigned to an int variable.
All other assignments (to result2, result3, result4) are invalid: division results always in a double which cannot be assigned to an int
(Lesson 3)
Question 22
Given the following program, which change will make the program run without error and print a 2?
int i = 5.0; => this will not help but instead add an additional error (by trying to assign a double value to an int variable)
double d = 2; => this would not change anything as d would still be a double (the value 2 would just be converted to 2.0)
int result = (int) i / d; => this would not help as the casting is applied before division, i.e. this code will first cast i to an int (i.e. no change) and then divide by a double leading again to a double as result
int result = i / (int) d; => this would work! The value d is casted to an int (which is allowed as we do it explicitely here) so the division becomes one of two int values resulting in an int as result!
int result = (double) i / d; => this would not help as it will just cast i to a double and then divide by a double leading again to a double as result
int result = (int)(i / d); => this would help! It still divides an int by a double resulting in a double. But then this resulting double is casted to an int (which is allowed as we do it explicitely here) and thus can be assigned to an int variable!
(Lesson 3)
The program will not compile and fail with an error
r=22!
r=22.0!
r=202!
r=202.0!
r=542.0!
Solution for Question 23
The correct answer is r=202.0!.
The first thing that is executed is the multiplication as it is enclosed by braces: 5 * 4 gives 20.
As there are no other braces, execution proceeds now from the left to the right.
First thing we see is a String, r= => thus all further + are interpreted as String concatenation!
(Lesson 3)
Remember:
The && condition is true if both conditions on its sides are true
The || condition is true if at least one condition of its both sides is true
The ! operator takes a boolean and switches it to its opposite (i.e. true to false, false to true)
(Lesson 3)
Question 25
Given the following program, what needs to be fixed so that it prints name and age of user?
classMain{publicstaticvoidmain(String[]args){Scannerscanner=newScanner();System.out.println("Enter your first name");Stringname=scanner.next();System.out.println("Enter your age");intage=scanner.next();System.out.println(name+" is "+age+" years old");}}
Add an import statement for Scanner class: import Scanner;
Add an import statement for Scanner class: import java.util.Scanner;
When creating Scanner, pass in System.in
When creating Scanner, pass in System.out
Read the name by using the nextLine() method
Read the age by using the nextLine() method
Read the age by using the nextInt() method
Solution for Question 25
Follow these rules when using Scanner to read input from the user:
always remember the import statement import java.util.Scanner at the beginning of your file
when creating a Scanner, pass in the System.in by doing new Scanner(System.in)
use the right method to read input: next() reads a single word and returns it as String, nextLine() a complete line and returns that as String. For other types, use the corresponding method (e.g. nextInt(), nextDouble(), nextBoolean() etc)
(Lesson 4)
Question 26
The following program will let the user enter his country and age but will immediately skip over the full name and will not wait for user to enter something. Which of the solutions could fix the problem?
importjava.util.Scanner;classMain{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.println("Enter your country of origin");Stringcountry=scanner.nextLine();System.out.println("Enter your age");intage=scanner.nextInt();System.out.println("Enter your full name");StringfullName=scanner.nextLine();System.out.println(fullName+" is "+age+" years old and comes from "+country);}}
Change ‘String country = scanner.nextLine();’ to ‘String country = scanner.next();’
Change ‘String fullName = scanner.nextLine();’ to ‘String fullName = scanner.next();’
Read in the fullName before all other input
Insert a new line with ‘scanner.nextLine();’ after ‘int age = scanner.nextInt();’
Solution for Question 26
The problem is due to how Scanner works internally => reread the part about Scanner in Lesson 4 for more details.
Let’s go through the answers:
“Change String country = scanner.nextLine(); to String country = scanner.next();’” => will not change anything, our problem is with reading fullName
“Change String fullName = scanner.nextLine(); to String fullName = scanner.next();” => this will allow us to enter something as full name => but it will end up only printing one word (the first name)
“Read in the fullName before all other input” => this will fix the issue as the problem we have only happens if we use nextLine() directly after next() (or a corresponding method for another type)
“Insert a new line with scanner.nextLine(); after int age = scanner.nextInt();” => this will also fix the problem - the additional scanner.nextLine() will clean the internal state of our Scanner that reading the fullName will work as epxected
(Lesson 4)
Question 27
Given the following program, which statements about it are true?
The program will not compile and fail with an error
The program will print nothing
The program will print 0, 1, 2, 3, 4
The program will print 0, 2, 4
The program will print 1, 3, 5
Solution for Question 31
It will print 0, 2 and 4.
Before the first iteration, i will be set to 0.
It will be then printed (i.e. the 0 as output).
Then i will be incremented by 2 and the next iteration happens - a 2 is printed.
Again, i is incremented by 2 and next iteration happens - a 4 is printed.
After the next increment by 2, i becomes bigger 5 => thus the loop will not be executed anymore.
(Lesson 5)
The program will not compile and fail with an error
The program will print nothing
The program will print 1, 2, 3, 4, 5
The program will print 5, 4, 3, 2, 1
The program will print 5, 4, 3, 2, 1, 0
The program will print 4, 3, 2, 1, 0
The program will run forever printing always 5
Solution for Question 32
It will print 5, 4, 3, 2, 1.
Before the first iteration, j will be set to the value of i, i.e. 5.
After that j is printed in each iteration.
And j is decremented in each iteration.
When j becomes 0, the loop stops.
Note: when a loop iteration ends, first the decrement happens and then the condition is checked (that’s why a 0 is not printed)
(Lesson 5)
The program will not compile and fail with an error
The program will print nothing
The program will print 1, 2, 3, 4, 5
The program will print 5, 4, 3, 2, 1
The program will print 5, 4, 3, 2, 1, 0
The program will print 4, 3, 2, 1, 0
The program will run forever printing always 5
Solution for Question 33
The program will run forever printing 5.
The assignment of the value of i only happens before the first loop iteration.
Any changes to i later do have no effect on j => thus j never changes during the loop!
(Lesson 5)
The program will always print a 0 as the first number
If the user enters a 42, the program will end immediately without any further output
The last number the program prints will always be 42
The program may run forever
The program may print nothing
Solution for Question 35
Let’s go through the statements:
The program will always print a 0 as the first number => this is obviously not true, the first number printed will be the first number the user entered
If the user enters a 42, the program will end immediately without any further output => this is false as the loop is executed at least once (number is initialized to 0 so the loop condition is initially true) and the loop body contains as last statement the printing of the number so each number the user enters must be printed
The last number the program prints will always be 42 => this is correct. The program ends when the user enters a 42 (number becomes 42). This is printed before the loop ends - so the last printed number must be 42
The program may run forever => this is true, the program will run forever if the user never enters 42
The program will print nothing => this is false, as there is no way that the program will print nothing!
(Lesson 5)
Question 36
Given the following program, which of the following statements are correct?
The program will always print a 0 as the first number
If the user enters a 42, the program will end immediately without any further output
The last number the program prints will always be 42
The program may run forever
The program may print nothing
Solution for Question 36
Let’s go through the statements:
The program will always print a 0 as the first number => this is obviously not true, the first number printed will be the first number the user entered
If the user enters a 42, the program will end immediately without any further output => this is true as anytime the user enters a number, the next action is the check of the loop condition. As the loop condition becomes false when number is 42, the print statement would not be executed
The last number the program prints will always be 42 => this is false as the loop would in this case end before any further print statement
The program may run forever => this is true, the program will run forever if the user never enters 42
The program may print nothing => this is true, it could happen if the user enters 42 as the first number
(Lesson 5)
The program will not compile and fail with an error
The program will print A, B, C
The program will print A, B, C, D, E
The program will print D, E, A, B, C
The program will print E, D, A, B, C
The program will print A, D, B, C, E
The program will print A, E, B, C, D
Solution for Question 37
It will print A, E, B, C, D.
It will start in main and executes the first print statement so A is printed.
It will then call method second and execute the statement in it, so E is printed.
It will then continue in main so B and C are printed.
It will then call method first and execute the statement in it, so D is printed.
(Lesson 6)
The program will not compile and fail with an error
The program will print A, B, Z, E, Z
The program will print A, Z, Z, E, Z
The program will print A, B, C, E, Y
The program will print A, B, Z, D, Z
The program will print A, B, Z, D, Y
Solution for Question 38
It will print A, B, Z, E, Z.
It will start in main and executes the first print statement so A is printed.
It will then call method test with parameters 2 and B - so when we execute test then n in test is 2 and text in test is B => so we print B.
It will then call method test with parameters n (i.e. the current value of n in main which is 22) and C - so when we execute test then n in test is 22 and text in test is C => so we print Z.
It will then call method test with parameters n (i.e. the current value of n in main which has changed to 1__) and _E - so when we execute test then n in test is 1 and text in test is E => so we print E.
It will then call method test with parameters 33 and Y - so when we execute test then n in test is 33 and text in test is Y => so we print Z.
(Lesson 6)
The program will not compile and fail with an error
The program will print A, B, Z, 1, Z, 1
The program will print A, B, C, 3, Z, 6
The program will print A, B, C, 4, Z, 13
The program will print A, B, C, 2, Z, 2
Solution for Question 39
It will print A, B, C, 4, Z, 13.
It will start in main and executes the first print statement so A is printed.
It will then call method test with parameters 2 and B - so when we execute test then n in test is 2 and text in test is B => so we print B. Also we return 3. In main, we set n to that value-
It will then call method test with parameters n (i.e. the current value of n in main which is 3) and C - so when we execute test then n in test is 3 and text in test is C => so we print C. We return now 4. In main, we set z to 4
We now print z i.e. we print 4.
It will then call method test with parameters n * z (i.e. the current value of n times the current value of z in main which is 3 * 4, i.e. 12) and K - so when we execute test then n in test is 12 and text in test is K => so we print Z. We return now 13 and set n to that value in main.
In main, we print now the current value of n, i.e. we print 13.
(Lesson 6)
The program will not compile and fail with an error
The program will print A, B, Z, 1, Z, 1
The program will print A, B, C, 3, Z, 6
The program will print A, B, C, 4, Z, 13
The program will print A, B, C, 2, Z, 2
Solution for Question 40
The program will not compile and will fail with an error.
The problem is in the test method - it declares a return type int - but we only have a return statement in the if but no return in the else.
If a method in Java has a return type, we must ensure that we return a value in each case!
(Lesson 6)
The program will not compile and fail with an error
The program will print A, B, Z, 1, Z, 1
The program will print A, B, C, 3, Z, 6
The program will print A, B, C, 4, Z, 13
The program will print A, B, C, 2, Z, 2
Solution for Question 41
The program will not compile and will fail with an error.
All calls to test have a problem:
the first call to test has the parameters switched - it calls with a String on first position and an int on second => but the test method expects an int as first parameter and a String as second
the second call to test fail to handle the returned value correctly - it tries to assign it to a String variable. But the return type of test is declared to be an int
the third call to test misses one parameter
(Lesson 6)
The program will not compile and fail with an error
The program will print A, B, Z, 1, Z, 1
The program will print A, B, C, 3, Z, 6
The program will print A, B, C, 4, Z, 13
The program will print A, B, C, 2, Z, 2
Solution for Question 42
The program will not compile and will fail with an error.
The problem is that the test method tries to access a variable z. But z is only defined in main and thus is also accessible inside main (i.e. inside the scope of the method main). The only way to give test access to value of z would be to pass it as a parameter to test!
(Lesson 6)
The program will not compile and fail with an error
The program will print 0 and abrakadabrasimsalabim
The program will print 3 and abrakadabrasimsalabim
The program will print 1 and rakadabrasimsalabim
The program will print 1 and rakadrasimsalim
The program will print 3 and rakadrasimsalim
The program will print 3 and im
Solution for Question 43
The program will print 3 and im - here is why:
The program counts the appearance of the value of variable sub in variable text by using indexOf (this will return the position in the string where the value appears or -1 if not found - thus the loop will run 3 times and count will be 3 at the end).
Additionally, every time the term is found, text is updated to only keep the part of the string after the found term.
Thus at the end, text will have the value everything after the last appereance of sub , i.e. im!
(Lesson 7)
Question 44
Given the following program, which of the statements is true?
The program will not compile and fail with an error
The program will print one word containing no vowels
The program will print one word where characters are in same order as entered
The program will print only capital letters
Given the input ‘HEllO’, the program will print ‘llh’
Solution for Question 44
The program will first turn the input to lower case letters.
It will then process the characters of the word in reverse order and filter out all vowels.
So the printed word will be all consonants of the entered word in reversed order and lower case!
(Lesson 7)
The program will not compile and fail with an error
The program will print 7 times_true_
The program will print 7 times_false_
The program will print false true false true false true true
The program will print false false false true false true true
The program will print false false false false true true true
Solution for Question 45
The program will print falsefalsefalsetruefalsetruetrue.
The checks using the == operator will anyway return false as they do not check the content of the String for equality but just if it the same object.
The equals method is case-sensitive so it will only return true when s2 is changed to lower cases (so the result is equal to s1).
The equalsIgnoreCase is not case-sensitive so it will return true as long as both strings contains the same letters (no matter if in upper or lower cases)
(Lesson 7)
Question 46
Given the following program, which of the statements are true?
The program will not compile and fail with an error
The program will always print the text exactly as the user has entered it
The program will only print the text exactly as the user has entered it if it did not start with an ‘a’ or did not end with an ‘b’ or did not contain a ‘c’
The program will print the entered text with each ‘c’ replaced by a ‘d’ if it started with ‘a’ and ended with ‘b’
The program will print the entered text with each ‘d’ replaced by a ‘c’ if it started with ‘a’ and ended with ‘b’
Solution for Question 46
The program will at the end always print the text as the user has entered it.
Note that the replace method does not change the String on that it is called - it creates a new copy of that where the replacements has happened, so text itself is never changed.
Additionally the program prints a version of text where each c is replaced by a d if text starts with a and ends with b.
(Lesson 7)
The program will not compile and fail with an error
The program will print ‘[“1”,”2”,3]’
The program will print ‘[1,2,3]’
The program will print ‘[“1”,”2”,”3”]’
Solution for Question 47
The program will not compile and fail with an error.
If you use an ArrayList you can specify what should be the type of elements that it should contain:
e.g. ArrayList<Integer> tells that the list can only contain integer values
e.g. ArrayList<String> tells that the list can only contain integer values
Thus you
cannot add a value of a different type to such a list
need to assign a value you get from the list to a variable of the correct type
If you do not follow these rules, your program will fail to compile
(Lesson 8)
Question 48
Given the following program, which of the statements are true?
The program will fail with an error after printing the last character
The program will end without any error
Solution for Question 48
The program will start by printing every second element in the list (i.e. b, d, f).
After it printed f, the value of i is 4. The loop will then increment i to 6.
Another loop iteration is executed as i is smaller than the size of the list (the number of elements in the list) which is 7.
Now inside the loop, it is tried to print the list element at index 7.
Note that the indices of a list start at index 0, so valid indices for the given list are from 0 to 6. 7 is too big and so the program fails!
(Lesson 8)
The program will not compile and fail with an error
The first loop will print ‘a’ and ‘b’ and the second loop ‘a’, ‘b’ and ‘c’
Both loops do exactly the same and print both ‘a’, ‘b’ and ‘c’
The first loop will print ‘a’, ‘b’ and ‘c’ while the second will print in one line ‘[a,b,c]’
Solution for Question 49
Both loops do exactly the same and print each element in the list.
Note that the for each loop is a shorter syntax for accessing every element in the loop (in each iteration, the loop variable , here word is set to the next element of the list).
(Lesson 8)
The program will print [a, c, c, a].
The test basically removes all occurrences of the given String in the given list.
It uses indexOf to get the index of the next appearance of the string in the list - if it is -1, the list does not contain anymore the string so the loop ends, else the element at that index is deleted.
Note: the remove element changes the given list directly!
(Lesson 8)
The program will print [a, c, d, b, e, f].
It will first assign the element at index 1 to s , i.e. s becomes b.
It will then remove the element at index 1 so words will become [a, c, d, e, f].
It will then add b at index 3 (i.e. add as 4th element into the list) so words will finally become [a, c, d, b, e, f].
(Lesson 8)