Java and Object-Oriented Programming Quiz
Test your knowledge of Java programming language features, object-oriented programming concepts, data types, operators, and compilation principles.
Questions
You have the following code in a file called Test.java class Base{ public static void main(String[] args){ System.out.println("Hello"); } } public class Test extends Base{} What will happen if you try to compile and run this?
- It will fail to compile.
- Runtime error
- Compiles and runs with no output.
- Compiles and runs printing "Hello"
What is the result of trying to compile and run the following code. public final static void main(String[] args){ double d = 10.0 / -0; if(d == Double.POSITIVE_INFINITY) System.out.println("Positive infinity"); else System.out.println("Negative infinity"); }
- output Positive infinity
- output Negative infinity
- Will fail to compile
- Runtime exception
What is the result that will be printed out ? void aMethod() { float f = (1 / 4) * 10; int i = Math.round(f); System.out.println(i); }
- 2
- 0
- 3
- 2.5
- 25
Which of the following are valid declarations?
- int i = 0XCAFE;
- boolean b = 0;
- char c = A;
- byte b = 128;
- char c = "A";
What is the result of trying to compile and run this program. public class Test{ public static void main(String[] args){ int[] a = {1}; Test t = new Test(); t.increment(a); System.out.println(a[a.length - 1]); } void increment(int[] i){ i[i.length - 1]++; } }
- Compiler error.
- Compiles and runs printing out 2
- Compiles and runs printing out 1
- An ArrayIndexOutOfBounds Exception at runtime
What will happen if you try to compile and run this ? public class Test{ static{ print(10); } static void print(int x){ System.out.println(x); System.exit(0); } }
- Compiler error.
- Will throw a NoSuchMethod error at runtime.
- It will compile and run printing out "10"
- It will run with no output.
- It will run and print "10" and then crash with an error.
The following code is legal? long longArr[]; int intArr[] = { 7 ,8 , 9}; longArr = intArr;
- True
- False
The range of a byte is from -127 to 128.
- True
- False
Identify the invalid assignments.
- float f = \u0038;
- long L2 = 2L;
- float f = 1.2;
- char c = '/u004E';
- byte b = 100;
What is the result of trying to compile and run the following code. public static void main(String[] args){ double d = 10 / 0; if(d == Double.POSITIVE_INFINITY) System.out.println("Positive infinity"); else System.out.println("Negative infinity"); }
- output Positive infinity
- output Negative infinity
- Will fail to compile
- Runtime exception
What attributes do all real world objects have?
- size and weight
- identity, state, and behavior
- state and behavior
- height and shape
What is another name for creating an object?
- initialization
- instantiation
- inheritance
- insubordination
String strA; String strB = new String("Cheese"); How many objects have been created in the above code snippet?
- 3
- 1
- 2
- 0
Short-circuit parameters are && and ||
- True
- False
What is the name for an application changing a readable programming language into a machine-readable language?
- Encoder
- Converter
- Compiler
- Translator
Which class is the highest superclass of Java?
- root
- object
- stem
- minor
Overloading, overriding and dynamic method binding are three types of what?
- Inheritance
- Polymorphism
- Generalization
- Abstraction
What was the first language to bring together all the features that characterize an object-oriented programming system?
- C++
- Java
- SmallTalk
- C
Setter is a special method, that sets property value. Used for private or protected variables or for some additional validation
- True
- False
Which of the following is not a C# keyword?
- a. if
- b. implements
- c. private
- d. delegates