Java Fundamentals for Android Development Quiz Questions and Answers

What would be the output of the following code? class MainClass { final String message() { return "Hello! "; } } class Main extends MainClass { public static void main(String[] args) { System.out.println(message()); } String message() { return "World! "; } }

Answer :
  • It will not compile because of line 10.

What would be the output of the following code? public class Solution { public static void main(String[] args) { for (int i = 44; i > 40; i--) { System.out.println( "f "); } } }

Answer :
  • ffff

Which operator would you use to find the remainder after division?

Answer :
  • %

When should you use a static method?

Answer :
  • When you want your method to be available independently of class instances

By implementing encapsulation, you cannot directly access the class's...... properties unless you are writing code inside the class itself.

Answer :
  • private

How many times will this code print "Hello World! "? class Main { public static void main(String[] args){ for (int i=0; i< 10; i=i++){ i+=1; System.out.println( "Hello World! "); } } }

Answer :
  • 10

What would be the output of the following code? class Main { public static void main(String[] args) { int x = 5; x = 10; System.out.println(x); } }

Answer :
  • 10

What would be the output of the following code? class Solution { public static void main(String[] args) { int a = 1; int b = 0; int c = a / b; System.out.println(c); } }

Answer :
  • It will throw an ArithmeticException

What method should be added to the Duck class to print the name Moby? public class Duck { private String name; Duck(String name) { this.name = name; } public static void main(String[] args) { System.out.println(new Duck( "Moby ")); } }

Answer :
  • public String toString() { return name; }

Which operator is used to concatenate Strings in Java?

Answer :
  • +