Java Fundamentals for Android Development Quiz Questions and Answers

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

Answer :
  • nothing - this will not compile.

Explanation :

The int type in Java can be used to represent any whole number from -2147483648 to 2147483647. Therefore this code will not compile as the number assigned to 'a' is larger than the int type can hold.

What would be the output of the following code? package mypackage; public class Math { public static int abs(int num){ return num < 0 ? -num : num; } } package mypackage.elementary; public class Math { public static int abs (int num) { return -num; } } import mypackage.Math; import mypackage.elementary.*; class Main { public static void main (String args[]){ System.out.println(Math.abs(123)); } }

Answer :
  • "123 "

Explanation :

The answer is "123 ". The abs() method evaluates to the one inside mypackage.Math class.

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.

Explanation :

Non-static method message() cannot be referenced from a static context.

Why are ArrayLists better than arrays?

Answer :
  • You don't have to decide the size of an ArrayList when you first make it

Declare a variable that holds the first four digits of PI.

Answer :
  • double pi = 3.141;

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

Answer :
  • %

Which choice is a disadvantage of inheritance?

Answer :
  • Classes related by inheritance are tightly coupled to each other

What kind of relationship does "extends " denote?

Answer :
  • is-a

Which access modifier makes variables and methods visible only in the class where they are declared?

Answer :
  • Private