Java Programming Fundamentals and OOP
Test your knowledge of Java programming including wrapper classes, autoboxing, method modifiers, inheritance, interfaces, constructors, and I/O operations.
Questions
Who can access a method if it is marked as protected internal ?
- Classes that are both in the same assembly and derived from the declaring class.
- Only methods that are in the same class as the method in question.
- Classes within the same assembly, and classes derived from the declaring class.
- Classes in different assembly
From the xml comments in the files in an assembly which compiler switch creates an xml file ?
- /text
- /doc
- /xml
- /help
Output of this program : class type { int num=12; void setnum(int num){ num=num; } int getnum() { return num; } public static void main(String[] args){ type obj=new type(); obj.setnum(10); System.out.print(obj.getnum()); } }
- 10
- 12
- compilation error
- none of these
Boolean size :
- 8 bit
- 1 bit
- machine dependent
- none of these
Correct way of method declaration within interface :
- void run();
- protected void go();
- public abstract set();
- none of these
Which one is true :
- interface declares constant and instance variables.
- The abstract modifier can be combined with the static modifier .
- native modifier can be applied only on method.
- synchronized modifier can be applied to method as well as class
- none of these
Which program will give correct output :
- class cons{ cons() { } public void cons(); public static void main (String[] aa){ cons b=new cons(); } }
- public class cons { ............... cons() { } public cons(int x,int y) { System.out.print(x +" "+ y); } public static void main (String[] aa){ cons b=new cons(); cons obj=new cons(4,2); }
- public class cons { public cons(int x) { System.out.print(x); } public static void main (String[] aa){ cons b=new cons(); } }
- class cons{ cons() { } public float foo () { double f = 32; return f; } public static void main (String[] aa){ cons b=new cons(); } }
Output of this program : class type { int num=12; void setnum(int num){ num=num; } int getnum() { return num; } public static void main(String[] args){ type obj=new type(); obj.setnum(10); System.out.print(obj.getnum()); } }
- 10
- 12
- compilation error
- none of these
Boolean size :
- 8 bit
- 1 bit
- machine dependent
- none of these
Correct way of method declaration within interface :
- void run();
- protected void go();
- static void get();
- public abstract set();
- none of these
Which one is true :
- interface declares constant and instance variables.
- The abstract modifier can be combined with the static modifier .
- native modifier can be applied only on method.
- synchronized modifier can be applied to method as well as class.
- none of these
Which program will give correct output :
- class cons{ cons() { } public void cons(); public static void main (String[] aa){ cons b=new cons(); } }
- public class cons { cons() { } public cons(int x,int y) { System.out.print(x +" "+ y); } public static void main (String[] aa){ cons b=new cons(); cons obj=new cons(4,2); } }
- public class cons { public cons(int x) { System.out.print(x); } public static void main (String[] aa){ cons b=new cons(); } }
- class cons{ cons() { } public float foo () { double f = 32; return f; } public static void main (String[] aa){ cons b=new cons(); } }
What is the output of the following code when compiled and run? Select two correct answers. public class TechnoSample { public static void main(String[] args){ for(int i = 0; i <> System.out.println(getPrimitive(127)); //line 1 } } public static int getPrimitive(byte b) { //line 2 return (short)(Math.random()*b); //line 3 } }
- Compilation error on line 1
- Compilation error on line 2
- Compilation error on line 3
- Line 3 compiles fine
- Prints 10 random numbers between 0 and 127
Select three correct statements.
- A static method may override another static method
- A static method cannot override a non-static method
- A non-static method cannot override a static method
- A non-static method may be overloaded by a static method
- A synchronized method cannot be overridden
Select three correct statements about the following code :-- public class TechnoSample { public static void main(String[] args) { TechnoSample myref = new TechnoSampleSub(); try{ myref.test(); } catch(Exception e){} } void test() throws Exception{ System.out.println("In TechnoSample"); throw new Exception(); } } class TechnoSample Sub extends TechnoSample { void test() { System.out.println("In TechnoSampleSub"); } }
- The try-catch block that encloses myref.test(); is mandatory for the code to compile
- Prints: In TechnoSample
- Prints: In TechnoSampleSub
- Method test() in class TechnoSampleSub has no obligation to declare a throws clause
- An exception is thrown at runtime
What will be output for the following program? public class Boxing2 { public static void main(String[] args) { byte b = 10; method(b); } static void method(int i){ System.out.println("Primitivae Type call"); } static void method(Integer i){ System.out.println("Wrapper Type Call"); } }
- Wrapper Type Call
- Primitive Type Call
- Compiler Error
- Compiles fine, throws runtime exception
What will be the output of the following program? package wrapper_boxing; public class WrapperTest2 { public static void main(String[] args) { Boolean bool1 = new Boolean(true); Boolean bool2 = new Boolean(false); Boolean bool3 = new Boolean("false"); Boolean bool4 = new Boolean(bool1); System.out.println(bool1.equals(bool4)); System.out.println(bool2 == bool3); System.out.println(bool1 == bool4); } }
- The program will not compile because the creation of bool4 object will cause compilation error.
- The program will produce the output 'true false false'.
- The program will produce the output 'true true true'.
- The program will output 'true false true'.
What will be the output of the following program? public class Ques09 { public static void main(String[] args) { Integer a = null; a = new Integer(10); int b = 20; a = b; a = 30; System.out.println(a++); short s = 100; a = s; System.out.println(a--); } }
- The program won't compile.
- The program will throw a run-time exception.
- The program will output '31 100'
- The program will output '31 99'
What will be the output of the following program? import java.io.ByteArrayInputStream; import java.io.InputStream; public class Ques04 { public static void main(String[] args) throws Exception{ byte buffer[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; InputStream inputStream = new ByteArrayInputStream(buffer); inputStream.read();inputStream.read(); inputStream.skip(4); System.out.println((char)inputStream.read()); } }
- The program will raise a run-time exception telling that it is not possible to skip bytes in the middle of a reading operation.
- The program will output 'd'.
- The program will output 'e'.
- The program will output 'g'.
It is possible to write the simple types directly to a binary file using the methods in the FileStream class.
- True
- False