Java Programming Fundamentals
Test your knowledge of Java programming language concepts including exception handling, compilation, inheritance, access modifiers, keywords, threading, and applets.
Questions
A static method can refer to any instance variable of the class.
- True
- False
Java runs on
- Windows
- Unix/Linux
- Macs
- All of the Above
An applet will run in almost any browser because
- The server has a built-in JVM
- The browser has a built-in JVM
- The source code is interpreted by the browser
- Applets don''t need a JVM.
What is an Applet?
- An interactive website
- A Java program that is run through a web browser
- A type of computer
- A type of fruit
What is Java?
- A type of coffee
- An object-oriented programming language
- An interactive website
- None of the above
The BorderLayout class provides static fields for
- Introducing new methods
- Adding components to specific areas of a container
- Starting an applet
- Specifying font size and color
What is the advantage of using import statements?
- To avoid having to declare variables
- To refer to a class without using prefixes
- To avoid calling methods
- To import the images you want to use
Which of the following statements about this code are true? class A extends Thread{ public void run(){ for(int i =0; i < 2; i++){ System.out.println(i); } } } public class Test{ public static void main(String argv[]){ Test t = new Test(); t.check(new A(){}); } public void check(A a){ a.start(); } }
- 0 0
- Compilation error, class A has no start method
- 0 1
- Compilation succeed but runtime exception
What is the output for the below code ? class A implements Runnable{ public void run(){ System.out.println("run-a"); } } 1. public class Test { 2. public static void main(String... args) { 3. A a = new A(); 4. Thread t = new Thread(a); 5. t.start(); 6. t.start(); 7. } 8. }
- run-a
- run-a run-a
- Compilation fails with an error at line 6
- Compilation succeed but Runtime Exception
What is the output for the below code ? public class Test { public static void main(String... args) { int a =5 , b=6, c =7; System.out.println("Value is "+ b +c); System.out.println(a + b +c); System.out.println("String "+(b+c)); } }
- Value is 67 18 String 13
- Value is 13 18 String 13
- Value is 13 18 String
- Compilation fails
What is the output for the below code ? 1. public class A { 2. int add(int i, int j){ 3. return i+j; 4. } 5.} 6.public class B extends A{ 7. public static void main(String argv[]){ 8. short s = 9; 9. System.out.println(add(s,6)); 10. } 11.}
- Compile fail due to error on line no 2
- Compile fail due to error on line no 9
- Compile fail due to error on line no 8
- 15
What is the output for the below code ? 1. public class Test { 2. public static void main(String[] args){ 3. int i = 010; 4. int j = 07; 5. System.out.println(i); 6. System.out.println(j); 7. } 8. }
- 8 7
- 10 7
- Compilation fails with an error at line 3
- Compilation fails with an error at line 5
Select correct answers
- The keyword extends is used to specify that an interface inherits from another interface.
- The keyword extends is used to specify that a class inherits from an interface.
- The keyword implements is used to specify that an interface inherits from another interface.
- The keyword implements is used to specify that a class inherits from an interface.
- The keyword implements is used to specify that a class inherits from another class.
Which of the following are reserved keywords in Java?
- public
- static
- void
- main
- String
How restrictive is the default accessibility compared to public, protected, and private accessibility in Java?
- Less restrictive than public
- More restrictive than public, but less restrictive than protected.
- More restrictive than protected, but less restrictive than private.
- More restrictive than private.
- Less restrictive than protected from within a package, and more restrictive than protected from outside a package.
Which of these are valid declarations of the main() method in order to start the execution of a Java application?
- static public void main(String[] args) { /* ... */ }
- public static int main(String[] args) { /* ... */ }
- public static void main(String args) { /* ... */ }
- public int main(Strings[] args, int argc) { /* ... */ }
In derived classes, in what order are the constructors called?
- from base to the derived class
- from derived to base
- only the derived class
- only the base class
Select correct answers
- A subclass must define all the methods from the superclass
- It is possible for a subclass to define a method with the same name and parameters as a method defined by the superclass.
- It is possible for a subclass to define a field with the same name as a field defined by the superclass.
- It is possible for two classes to be the superclass of each other.
What will be the result of attempting to compile this Java class?
- The class will fail to compile, since the class Other has not yet been declared when referenced in class AClass.
- The class will fail to compile, since import statements must never be at the very top of a file.
- The class will fail to compile, since the package declaration can never occur after an import statement.
- The class will fail to compile, since the class Other must be defined in a file called Other.java.
- The class will fail to compile, since the class Other must be declared public.
If an exception is not caught, the finally block will run
- True
- False