Java Quiz
What is printed by the following statement? System.out.print("Hello,\nworld!"); (a) Hello, \nworld! (b) Hello, world! (c) (d) "Hello, \nworld!" (e) None of the above.
(c) Reason: The statement System.out.print("Hello,\nworld!"); gives output as
The default value of a static integer variable of a class in Java is, (a) 0 (b) 1 (c) Garbage value (d) Null (e) -1.
0 Reason: The default value of a static integer variable of a class in Java is 0.
In Java, a try block should immediately be followed by one or more .................... blocks. (a) Throw (b) Run (c) Exit (d) Catch (e) Error.
Answer : (d) Catch Reason : Try must be followed by catch block in exceptions in java.
Among these expressions, which is(are) of type String? (a) "0" (b) "ab" + "cd" (c) '0' (d) Both (A) and (B) above (e) (A), (B) and (C) above.
Both (A) and (B) above Reason: Strings are "0" and "ab" + "cd" .
Consider the two methods (within the same class) public static int foo(int a, String s) { s = "Yellow"; a=a+2; return a; } public static void bar() { int a=3; String s = "Blue"; a = foo(a,s); System.out.println("a="+a+" s="+s); } public static void main(String args[]) { bar(); } What is printed on execution of these methods? (a) a = 3 s = Blue (b) a = 5 s = Yellow (c) a = 3 s = Yellow (d) a = 5 s = Blue (e) none of the above.
a = 5 s = Blue Reason: 'a' value is returned from the method and so it is 5. But the string will remain same, as it is passed by value to the method.
Which of the following is true? (a) A finally block is executed before the catch block but after the try block. (b) A finally block is executed, only after the catch block is executed. (c) A finally block is executed whether an exception is thrown or not. (d) A finally block is executed, only if an exception occurs. (e) None of the above.
A finally block is executed whether an exception is thrown or not. Reason: A finally block is executed whether an exception is thrown or not is correct.
Which statement is not true in java language? (a) A public member of a class can be accessed in all the packages. (b) A private member of a class cannot be accessed by the methods of the same class. (c) A private member of a class cannot be accessed from its derived class. (d) A protected member of a class can be accessed from its derived class.
A private member of a class cannot be accessed by the methods of the same class. Reason: Private members of a class can be accessed by the methods within the same class.
Which one of the following is not true? (a) A class containing abstract methods is called an abstract class. (b) Abstract methods should be implemented in the derived class. (c) An abstract class cannot have non-abstract methods. (d) A class must be qualified as 'abstract' class, if it contains one abstract method. (e) None of the above.
An abstract class cannot have non-abstract methods. Reason: An abstract class can contain both abstract and non-abstract methods.
Which of the following is not true? (a) An interface can extend another interface. (b) A class which is implementing an interface must implement all the methods of the interface. (c) An interface can implement another interface. (d) An interface is a solution for multiple inheritance in java. (e) None of the above.
An interface can implement another interface. Reason: An interface can extend another interface but not implement.
Basic Java language functions are stored in which of the following java package? (a) java.lang (b) java.io (c) java.net (d) java.util (e) java.awt.
Answer : (a) Reason : The basic language functions are stored in a package inside of the java package called java.lang.
Analyze the following code: public class Test { public static void main(String[] args) { int[] oldList = {1, 2, 3, 4, 5}; reverse(oldList); for (int i = 0; i < oldList.length; i++) System.out.print(oldList[i] + " "); } public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList; } } (a) The program displays 1 2 3 4 5 (b) The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException (c) The program displays 5 4 3 2 1 (d) The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException (e) The program displays 54321 and does not raise an arrayIndexOutOfBoundsException.
Answer : (a) Reason : The contents of the array oldList have not been changed as result of invoking the reverse method.
Analyze the following code public class Test { public static void main(String[] args) { System.out.println(m(2)); } public static int m(int num) { return num; } public static void m(int num) { System.out.println(num); } } (a) The program has a syntax error because the two methods m have the same signature (b) The program has a syntax error because the second m method is defined, but not invoked in the main method (c) The program runs and prints 2 once (d) The program runs and prints 2 twice (e) The program runs and prints 2 thrice.
Answer : (a) Reason : You cannot override the methods based on the type returned.
Object oriented inheritance models the (a) "is a kind of" relationship (b) "has a" relationship (c) "want to be" relationship (d) inheritance does not describe any kind of relationship between classes (e) "contains" of relationship.
Answer : (a) "is a kind of" relationship Reason : As it models generalization relationship it takes "is a kind of "keyword.
The wrapping up of data and functions into a single unit is called (a) Encapsulation (b) Abstraction (c) Data Hiding (d) Polymorphism (e) Message passing.
Answer : (a) Encapsulation Reason : Data encapsulation is the method of wrapping up of data and functions into a single unit.
Consider, public class MyClass { public MyClass(){/*code*/} // more code... } To instantiate MyClass, you would write? (a) MyClass mc = new MyClass(); (b) MyClass mc = MyClass(); (c) MyClass mc = MyClass; (d) MyClass mc = new MyClass; (e) The constructor of MyClass should be defined as, public void MyClass(){/*code*/}.
Answer : (a) MyClass mc = new MyClass(); Reason: An object is created by using a new operator.
The correct order of the declarations in a Java program is, (a) Package declaration, import statement, class declaration (b) Import statement, package declaration, class declaration (c) Import statement, class declaration, package declaration (d) Class declaration, import statement, package declaration (e) Class declaration, package declaration, import statement.
Answer : (a) Package declaration, import statement, class declaration Reason: First the package name is defined. Then the import statements if any. And then the class declaration goes on.
Given a class named student, which of the following is a valid constructor declaration for the class? (a) Student (student s) { } (b) Student student ( ) { } (c) Private final student ( ) { } (d) Void student ( ) { } (e) Static void student(){ }.
Answer : (a) Student (student s) { } Reason : A constructor cannot specify any return type, not even void. A constructor cannot be final, static or abstract.
What is byte code in the context of Java? (a) The type of code generated by a Java compiler. (b) The type of code generated by a Java Virtual Machine. (c) It is another name for a Java source file. (d) It is the code written within the instance methods of a class. (e) It is another name for comments written within a program.
Answer : (a) The type of code generated by a Java compiler. Reason: Java compiler compiles the source code file and converts it into a class file, which consists of byte code.
In Java, declaring a class abstract is useful (a) To prevent developers from further extending the class (b) When it doesn't make sense to have objects of that class (c) When default implementations of some methods are not desirable (d) To force developers to extend the class not to use its capabilities (e) When it makes sense to have objects of that class.
Answer : (b) Reason : As other Choices are contradicting the meaning of Abstract Class .
Consider the following Java program: public class MyClass { private int myVlaue = 5; public void printMyVlaue() { System.out.println(myVlaue); } public void setMyVlaue(int myVlaue) { System.out.println(myVlaue); this.myVlaue = myVlaue; } public static void main(String[] args) { MyClass myClass1 = new MyClass(); myClass1.setMyVlaue(10); myClass1.printMyVlaue(); } } Which of the following will the output be? (a) 5 10 (b) 10 10 (c) 10 5 (d) 5 5 (e) 15 5
Answer : (b) Reason : Based on the methods call the out put is 10 10.
Which of the following has a method names flush( )? (a) Input stream (b) Output Stream (c) Reader stream (d) Input reader stream (e) Input output stream.
Answer : (b) Reason : Output stream has the method flush ( ) that flushes the output stream.
Which of these field declarations are legal within the body of an interface? (a) Private final static int answer = 42 (b) public static int answer=42 (c) final static answer =42 (d) int answer (e) No error.
Answer : (b) Reason : The remaining are all illegal.
Analyze the following code. public class Test { public static void main(String[] args) { System.out.println(max(1, 2)); } public static double max(int num1, double num2) { System.out.println("max(int, double) is invoked"); if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { System.out.println("max(double, int) is invoked"); if (num1 > num2) return num1; else return num2; } } (a) The program cannot compile because you cannot have the print statement in a non-void method (b) The program cannot compile because the compiler cannot determine which max method should be invoked (c) The program runs and prints 2 followed by "max(int, double)" is invoked (d) The program runs and prints 2 followed by "max(double, int)" is invoked (e) The program runs and prints "max(int, double) is invoked" followed by 2.
Answer : (b) Reason : This is known as ambiguous method invocation
What will the following program print when it is executed? public class Practice11 { public static void main(String args[]) { int i = 0; while (i < 3) { if( i++ == 0 ) System.out.print("Merry"); if( i == 1) System.out.print("Merr"); if( ++i == 2) System.out.print("Mer"); else System.out.print("Oh no!"); } } } (a) Merry Merr Mer (b) MerryMerrMerOh no! (c) MerrMerOh no! (d) MerryMerrMerOh no! MerrMerOh no! MerOh no! (e) Merry.
Answer : (b) Reason : This program exercises your ability to walk through if() statements as well as post and pre increment. Be sure to carefully walk through the code and don't forget the while() loop.
Consider the following Java program : public class Compute { public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 = = 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? (a) 55 (b) 30 (c) 25 (d) 35 (e) 45.
Answer : (b) 30 Reason : B is the right choice remaining all are contradictory to the execution of program
Which statement is true regarding an object? (a) An object is what classes instantiated are from (b) An object is an instance of a class (c) An object is a variable (d) An object is a reference to an attribute (e) An object is not an instance of a class.
Answer : (b) An object is an instance of a class Reason : An object is an instance of a class. Objects are created from class definitions that implement abstractions.
Java compiler javac translates Java source code into ........................... (a) Assembler language (b) Byte code (c) Bit code (d) Machine code (e) Platform dependent code.
Answer : (b) Byte code Reason : Byte Code is the file which is generated after java source file is compiled.
What will be printed as the output of the following program? public class testincr { public static void main(String args[]) { int i = 0; i = i++ + i; System.out.println("I = " +i); } } (a) I = 0 (b) I = 1 (c) I = 2 (d) I = 3 (e) Compile-time Error.
Answer : (b) I = 1 Reason: 1 The execution goes on like this: int i = 0; // i becomes 0 i = 0 + i; // now, i becomes 1 i = 0 + 1; // perform addition and assign 1 to i.
Consider the following statements about Java packages: I. Packages don't provide a mechanism to partition all class names into more manageable chunks. II. Packages provide a visibility control mechanism. III. One of the important properties of a package is that all classes defined inside a package is accessible by code outside that package. IV. The .class files for classes declared to be part of a package can be stored in multiple directories. Which of them is correct? (a) Only (I) above (b) Only (II) above (c) Only (III) above (d) Only (IV) above (e) All (I), (II), (III) and (IV) above are wrong.
Answer : (b) Only (II) above Reason : Except b above all are contradictory to the functionality of package.
When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the (a) Super class (b) Subclass (c) Compiler will choose randomly (d) Interpreter will choose randomly (e) None of the above.
Answer : (b) Subclass Reason: The compiler will always selects the version of the method in subclass. superclass method can be called by explicitly assigning super reference.
Identify, from among the following, the incorrect descriptions related to Java : (a) Java Virtual Machine translates byte code into its own system's machine language and runs the resulting machine code (b) The arithmetic operations *, /, %, + and - have the same level of precedence (c) Comments do not cause any action to be performed during the program execution (d) All variables must be given a type when they are declared (e) Java variable names are case-sensitive.
Answer : (b) The arithmetic operations *, /, %, + and - have the same level of precedence Reason : The rule of precedence for arithmetic operations is *,/,+or- from left to right.
In object oriented programming, composition relates to (a) The use of consistent coding conventions (b) The organization of components interacting to achieve a coherent, common behavior (c) The use of inheritance to achieve polymorphic behavior (d) The organization of components interacting not to achieve a coherent common behavior (e) The use of data hiding to achieve polymorphic behavior.
Answer : (b) The organization of components interacting to achieve a coherent, common behavior Reason : Composition is about an object that is made up of other objects, referred as "containment. Composition implies that the life cycles are more strongly linked, that means Whole is responsible for the life time of Part.
In Java, a character constant's value is its integer value in the ...........................character set. (a) EBCDIC (b) Unicode (c) ASCII (d) Binary (e) BCD.
Answer : (b) Unicode Reason : Java uses Unicode format.
You read the following statement in a Java program that compiles and executes. submarine.dive(depth); What can you say for sure? (a) depth must be an int (b) dive must be a method. (c) dive must be the name of an instance field. (d) submarine must be the name of a class (e) submarine must be a method.
Answer : (b) dive must be a method. Reason: The other choices can be allowed, but not 'must'
What is the output of the following code: class eq { public static void main(String args[]) { String s1 = "Hello"; String s2 = new String(s1); System.out.println(s1==s2); } } (a) true (b) false (c) 0 (d) 1 (e) Hello.
Answer : (b) false Reason: Since , the contents of the two String objects are identical, but they are distinct objects.
The java run time system automatically calls this method while garbage collection. (a) finalizer() (b) finalize() (c) finally() (d) finalized() (e) none of the above.
Answer : (b) finalize() Reason: 'finalize()' method is automatically called by the java compiler before destroying the object to free any resources.
. Analyze the following code: public class Test { public static void main(String[] args) { int[] x = new int[5]; int i; for (i = 0; i < x.length; i++) x[i] = i; System.out.println(x[i]); } } (a) The program displays 0 1 2 3 4 (b) The program displays 4 (c) The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException (d) The program has syntax error because i is not defined in the last statement in the main method (e) The program displays 1 2 3 4 5.
Answer : (c) Reason : After the for loop i is 6. x [5] is out of bounds.
What is garbage collection in the context of Java? (a) The operating system periodically deletes all of the java files available on the system. (b) Any package imported in a program and not used is automatically deleted. (c) When all references to an object are gone, the memory used by the object is automatically reclaimed. (d) The JVM checks the output of any Java program and deletes anything that doesn't make sense. (e) When all references to an object are gone the memory used by the object is not reclaimed.
Answer : (c) Reason : As the remaining all contradict the functionality of JVM.
Which of the following is a member of the java.lang package? (a) List (b) Queue (c) Math (d) Stack (e) Process.
Answer : (c) Reason : Math is a member of the java.lang package whereas the remaining are not.
The concept of multiple inheritance is implemented in Java by I. Extending two or more classes. II. Extending one class and implementing one or more interfaces. III. Implementing two or more interfaces. (a) Only (II) (b) (I) and (II) (c) (II) and (III) (d) Only (I) (e) Only (III).
Answer : (c) Reason : The remaning are not opt solutions for the concept of multiple inheritance.
What is the error in the following class definitions? Abstract class xy { abstract sum (int x, int y) { } } (a) Class header is not defined properly. (b) Constructor is not defined. (c) Method is not defined properly (d) Method is defined properly (e) No error.
Answer : (c) Reason : method is not defined properly.
Given the following code, which statement is true? class MyClass { public static void main(String[] args) { int k = 0; int l = 0; for (int i=0;i<=3;i++) { k++; if (i==2) break; l++; } System.out.println(k+", "+l); } } (a) The program will fail to compile (b) The program will print 3, 3 when run (c) The program will print 4, 3 when run if break is replaced by continue (d) The program will fail to compile if break is replaced by return (e) The program will fail to compile if break is simply removed.
Answer : (c) Reason: As it stands, the program will compile correctly and will print "3, 2" when run. If the break statement is replaced with a continue statement, the loop will perform all four iterations and will print "4, 3". If the break statement is replaced with a return statement, the whole method will end when i equals 2, before anything is printed. If the break statement is simply removed, leaving the empty statement (;), the loop will complete all four iterations and will print "4, 4".
Given the following source code, which comment line can be uncommented without introducing errors? abstract class MyClass { abstract void f(); final void g() {} // final void h() {} // (1) protected static int i; private int j; } final class MyOtherClass extends MyClass { // MyOtherClass(int n) { m = n; } //(2) public static void main(String[] args) { MyClass mc = new MyOtherClass(); } void f() { } void h() { } // void k() { i++; } // (3) // void l() { j++; } // (4) int m; } (a) (1) (b) (2) (c) (3) (d) (4) (e) None of the above.
Answer : (c) Reason: The line void k() { i++; } can be re-inserted without introducing errors. Re-inserting the line (1) will cause the compilation to fail, since MyOtherClass will try to override a final method. Re-inserting line (2) will fail, since MyOtherClass will no longer have a default constructor. The main() method needs to call the default constructor. Re-inserting line (3) will work without any problems, but re-inserting line (4) will fail, since the method will try to access a private member of the superclass.
What is the printout of the third println statement in the main method? public class Foo { int i; static int s; public static void main(String[] args) { Foo f1 = new Foo(); System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); Foo f2 = new Foo(); System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); Foo f3 = new Foo(); System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); } public Foo() { i++; s++; } } (a) f3.i is 1 f3.s is 1 (b) f3.i is 1 f3.s is 2 (c) f3.i is 1 f3.s is 3 (d) f3.i is 3 f3.s is 1 (e) f3.i is 3 f3.s is 3.
Answer : (c) Reason: i is an instance variable and s is static, shared by all objects of the Foo class.
Consider the following data types in Java : I. Int II. Boolean III. Double IV. String V. Array. Which of them are simple/primitive data types? (a) Both (I) and (II) above (b) (I), (II), (III) and (IV) above (c) (I), (II) and (III) above (d) (II) and (III) above (e) All (I), (II), (III), (IV) and (V) above.
Answer : (c) (I), (II) and (III) above Reason : String and array don't fall into the category of simple datatypes.
What is the output of the following program: public class testmeth { static int i = 1; public static void main(String args[]) { System.out.println(i+" , "); m(i); System.out.println(i); } public void m(int i) { i += 2; } } (a) 1 , 3 (b) 3 , 1 (c) 1 , 1 (d) 1 , 0 (e) none of the above.
Answer : (c) 1 , 1 Reason: Parameter values are passed by value in the calling of a method, and so a copy of the value is created in the method, and the original value is not affected by the method call.
Identify, from among the following, the incorrect variable name(s). (a) _theButton (b) $reallyBigNumber (c) 2ndName (d) CurrentWeatherStateofplanet (e) my2ndFont.
Answer : (c) 2ndName Reason : A variable name should not start with numeric digit,
A process that involves recognizing and focusing on the important characteristics of a situation or object is known as: (a) Encapsulation (b) Polymorphism (c) Abstraction (d) Inheritance (e) Object persistence.
Answer : (c) Abstraction Reason : The insulation of data from direct access by the program is called as the data abstraction.Data abstraction also called as data hiding, is the mechanism where by the code and the data it manipulates are wrapped into a single unit.
.................... are used to document a program and improve its readability. (a) System cells (b) Keywords (c) Comments (d) Control structures (e) Blocks.
Answer : (c) Comments Reason : Comments improve the readability of the program
In java, objects are passed as (a) Copy of that object (b) Method called call by value (c) Memory address (d) Constructor (e) Default constructor.
Answer : (c) Memory address Reason : Objects are passed as memory address but not by the constructor etc.,
In a class definition, the special method provided to be called to create an instance of that class is known as a/an (a) Interpreter (b) Destructor (c) Constructor (d) Object (e) Compiler.
Answer : (c) Object Reason : According to the concept of Constructor C is right choice.
Which of the following does not belong: If a class inherits from some other class, it should (a) Make use of the parent class's capabilities (b) Over-ride or add the minimum to accomplish the derived class' purpose (c) Over-ride all the methods of its parent class (d) Make sure the result "IS-A-KIND-OF" its base class (e) Make sure the result "contains" its base class.
Answer : (c) Over-ride all the methods of its parent class Reason : In a class hierarchy, when a method in a subclass has the same method signature as that of the superclass, then the method in the subclass is said to "override" the method in superclass. When an overridden method exists in the subclass, the subclass objects always refer its own. The subclass overridden method hides ( or blocks ) that of the superclass method.
What is garbage collection in the context of Java? (a) The operating system periodically deletes all the java files available on the system. (b) Any package imported in a program and not used is automatically deleted. (c) When all references to an object are gone, the memory used by the object is automatically reclaimed. (d) The JVM checks the output of any Java program and deletes anything that doesn't make sense. (e) Janitors working for Sun Micro Systems are required to throw away any Microsoft documentation found in the employees' offices.
Answer : (c) When all references to an object are gone, the memory used by the object is automatically reclaimed. Reason: Garbage collection in the context of Java is when all references to an object are gone, the memory used by the object is automatically reclaimed.
A protected member can be accessed in, (a) a subclass of the same package (b) a non-subclass of the same package (c) a non-subclass of different package (d) a subclass of different package (e) the same class. Which is the false option?
Answer : (c) a non-subclass of different package Reason: A protected member cannot be accessed in non-subclasses of different packages.
For what values respectively of the variables gender and age would the Java expression gender == 1 && age >= 65 become true? (a) gender = 1, age = 60 (b) gender = 1, age = 50 (c) gender = 1, age = 65 (d) gender = 0, age = 70 (e) gender = 0, age = 55.
Answer : (c) gender = 1, age = 65 Reason : && is the short circuit operator which sees that all conditions should be true then only it will evaluate to true.
A package is a collection of (a) Classes (b) Interfaces (c) Editing tools (d) Classes and interfaces (e) Editing tools and interfaces.
Answer : (d) Reason : Editing tools are not the part of packages.So d is the correct Answer.
A method within a class is only accessible by classes that are defined within the same package as the class of the method. Which one of the following is used to enforce such restriction? (a) Declare the method with the keyword public (b) Declare the method with the keyword private (c) Declare the method with the keyword protected (d) Do not declare the method with any accessibility modifiers (e) Declare the method with the keyword public and private.
Answer : (d) Reason : The desired accessibility is package accessibility, which is the default accessibility for members that have no accessibility modifier. Package is not an accessibility modifier.
Consider the following statement(s) about Java: I. All white-space characters (blanks) are ignored by the compiler. II. Java keywords can be used as variable names. III. An identifier does not begin with a digit and does not contain any spaces. IV. The execution of Java applications begins at method main. Which of them is correct? (a) Both (I) and (III) above (b) Both (II) and (IV) above (c) Both (I) and (II) above (d) (III) and (IV) above (e) All (I), (II), (III) and (IV) above.
Answer : (d) (III) and (IV) above Reason : Remaining all are contradictory to the statements with respect to java.
Consider the following program: class prob1{ int puzzel(int n){ int result; if (n==1) return 1; result = puzzel(n-1) * n; return result; } } class prob2{ public static void main(String args[]) { prob1 f = new prob1(); System.out.println(" puzzel of 6 is = " + f.puzzel(6)); } } Which of the following will be the output of the above program? (a) 6 (b) 120 (c) 30 (d) 720 (e) 12.
Answer : (d) 720 Reason : 720 is the output after the execution of the program
Which of the following is TRUE? (a) In java, an instance field declared public generates a compilation error. (b) int is the name of a class available in the package java.lang (c) Instance variable names may only contain letters and digits. (d) A class has always a constructor (possibly automatically supplied by the java compiler). (e) The more comments in a program, the faster the program runs.
Answer : (d) A class has always a constructor (possibly automatically supplied by the java compiler). Reason: A class will always have a constructor, either provided by the user or a default constructor provided by the compiler.
Consider the following class definition: public class MyClass { private int value; public void setValue(int i){ /* code */ } // Other methods... } The method setValue assigns the value of i to the instance field value. What could you write for the implementation of setValue? (a) value = i; (b) this.value = i; (c) value == i; (d) Both (A) and (B) and above (e) (A), (B) and (C) above.
Answer : (d) Both (A) and (B) and above Reason: '==' is a comparison operator.
An overloaded method consists of, (a) The same method name with different types of parameters (b) The same method name with different number of parameters (c) The same method name and same number and type of parameters with different return type (d) Both (a) and (b) above (e) (a), (b) and (c) above.
Answer : (d) Both (a) and (b) above Reason: Even though the return type varies, it will not be considered for overloading concept. Only method name and parameters are considered.
Mark the incorrect statement from the following: (a) Java is a fully object oriented language with strong support for proper software engineering techniques (b) In java it is not easy to write C-like so called procedural programs (c) In java language objects have to be manipulated (d) In java language error processing is built into the language (e) Java is not a language for internet programming.
Answer : (d) In java language error processing is built into the language Reason : Java is basically designed for compiler construction but later on it is also being used for internet programming.
In object oriented programming new classes can be defined by extending existing classes. This is an example of: (a) Encapsulation (b) Interface (c) Composition (d) Inheritance (e) Aggregation.
Answer : (d) Inheritance Reason : Inheritance is a mechanism that enables one class to inherit all of the behaviour (methods ) and attributes (instance variables) of another class. A class that inherits from another class is called a subclass and the class that gives the inheritance is called a superclass.
In object-oriented programming, the process by which one object acquires the properties of another object is called (a) Encapsulation (b) Polymorphism (c) Overloading (d) Inheritance (e) Overriding.
Answer : (d) Inheritance Reason : Inheritance is the mechanism in which one object acquires the properties of another object.
The blank space in the following sentence has to be correctly filled : Members of a class specified as .................... are accessible only to methods of that class. (a) Protected (b) Final (c) Public (d) Private (e) Static.
Answer : (d) Private Reason : Private access specifier property says that members of class of private are accessible only to the methods of that class.
Which of the following statements about Java Threads is correct? (a) Java threads don't allow parts of a program to be executed in parallel (b) Java is a single-threaded language (c) Java's garbage collector runs as a high priority thread (d) Ready, running and sleeping are three states that a thread can be in during its life cycle (e) Every java application is not multithreaded.
Answer : (d) Ready, running and sleeping are three states that a thread can be in during its life cycle Reason : Ready, running and sleeping which are three states that a thread can be in during its life cycle.
All exception types are subclasses of the built-in class (a) Exception (b) RuntimeException (c) Error (d) Throwable (e) None of the above.
Answer : (d) Throwable Reason: Throwable is the super class of all the exception classes.
Suppose static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); n--; } } What is the printout of the call Print('a', 4)? (a) aaaaa (b) aaaa (c) aaa (d) aa (e) nvalid call.
Answer : (e) Reason : Invalid call because char 'a' cannot be passed to string
Consider the following Java program to answer this Question: public class Main { private String s; Main() { this.print("Main() constructor"); } static void print(String s) { System.out.println(s); this.s = s; } String getLastPrint() { return s; } public static void main(String[] args) { Main m = new Main(); m.print("Hello"); Main.print("Hello World"); String last = m.getLastPrint(); What would be the output of the above program if it is executed? (a) Hello (b) Hello World (c) Hello Hello Hello World (d) Hello Hello World Hello (e) error
Answer : (e) Reason : Not a valid code
What would be the result of attempting to compile and run the following program? class MyClass { static MyClass ref; String[] arguments; public static void main(String[] args) { ref = new MyClass(); ref.func(args); } public void func(String[] args) { ref.arguments = args; } } (a) The program will fail to compile, since the static method main() cannot have a call to the non-static method func() (b) The program will fail to compile, since the non-static method func() cannot access the static variable ref (c) The program will fail to compile, since the argument args passed to the static method main() cannot be passed on to the non-static method func() (d) The program will compile, but will throw an exception when run (e) The program will compile and run successfully.
Answer : (e) Reason: An object reference is needed to access non-static members. Static methods do not have the implicit object reference this, and must always supply an explicit object reference when referring to non-static members. The static method main() refers legally to the non-static method func() using the reference variable ref. Static members are accessible both from static and non-static methods, using their simple names.
What will be the result of attempting to compile and run the following code? class MyClass { public static void main(String[] args) { boolean b = false; int i = 1; do { i++; b = !b; } while (b); System.out.println(i); } } (a) The code will fail to compile, since b is an invalid conditional expression in the do-while statement (b) The code will fail to compile, since the assignment b = !b is not allowed (c) The code will compile without error and will print 1 when run (d) The code will compile without error and will print 2 when run (e) The code will compile without error and will print 3 when run.
Answer : (e) Reason: The loop body is executed twice and the program will print 3. The first time the loop is executed, the variable i changes from 1 to 2 and the variable b changes from false to true. Then the loop condition is evaluated. Since b is true, the loop body is executed again. This time the variable i changes from 2 to 3 and the variable b changes from true to false. The loop condition is now evaluated again. Since b is now false, the loop terminates and the current value of i is printed.
What, if anything, is wrong with following code? void test(int x) { switch (x) { case 1: case 2: case 0: default: case 4: } } (a) The variable x does not have the right type for a switch expression (b) The case label 0 must precede case label 1 (c) Each case section must end with a break statement (d) The default label must be the last label in the switch statement (e) There is nothing wrong with the code.
Answer : (e) Reason: There is nothing wrong with the code. The case and default labels do not have to be specified in any specific order. The use of the break statement is not mandatory, and without it the control flow will simply fall through the labels of the switch statement.
A constructor (a) Must have the same name as the class it is declared within. (b) Is used to create objects. (c) May be declared private (d) Both (A) and (B) above (e) (a), (b) and (c) above.
Answer : (e) (a), (b) and (c) above. Reason: A constructor · Must have the same name as the class it is declared within. · Is used to create objects. · May be declared private.
An abstract data type typically comprises a ............... and a set of .................. respectively. (a) Data representation, classes (b) Database, operations (c) Data representation, objects (d) Control structure, operations (e) Data representation, operations.
Answer : (e) Data representation, operations Reason : An abstract data type contains data representation and operations
Consider the following statements: I. A class can be declared as both abstract and final. II. A class declared as final can be extended by defining a sub-class. III. Resolving calls to methods dynamically at run-time is called late binding. IV. The class Object defined by Java need not be a super class of all other classes. Identify the correct statement from the following: (a) Both (I) and (II) above (b) Both (III) and (IV) above (c) Both (I) and (III) above (d) Both (II) and (IV) above (e) Only (III) above.
Answer : (e) Only (III) above. Reason : Except e above all are contradictory to the functionality of classes concept of java
Polymorphism (a) Is not supported by Java (b) Refers to the ability of two or more objects belonging to different classes to respond to exactly the same message in different class-specific ways (c) Simplifies code maintenance (d) Not simplifies code manintenance (e) Refers to the ability of two or more objects belonging to different classes to respond to exactly the same message in different class -specific ways and simplifies code maintenance.
Answer : (e) Refers to the ability of two or more objects belonging to different classes to respond to exactly the same message in different class -specific ways and simplifies code maintenance. Reason : As Polymorphism refers to the ability of two or more objects belonging to different classes to respond to exactly the same message in different class-specific ways. It also simplifies code manintenance.
Re-implementing an inherited method in a sub class to perform a different task from the parent class is called (a) Binding (b) Transferring (c) Hiding (d) Coupling (e) extending.
Answer : (e) extending. Reason : Extending is the mechanism in which reimplementing an inherited method in a sub class to perform a different task from the parent class.
Consider the following code fragment Rectangle r1 = new Rectangle(); r1.setColor(Color.blue); Rectangle r2 = r1; r2.setColor(Color.red); After the above piece of code is executed, what are the colors of r1 and r2 (in this order)? (a) Color.blue Color.red (b) Color.blue Color.blue (c) Color.red Color.red (d) Color.red Color.blue (e) None of the above.
Color.red Color.red Reason: Both r1 and r2 are referring the same object of Rectangle class. So, finally the Color of the object is changed to red.
The fields in an interface are implicitly specified as, (a) static only (b) protected (c) private (d) both static and final (e) none of the above.
both static and final Reason: The fields in an interface are implicitly specified as both static and final.
What is the type and value of the following expression? (Notice the integer division) -4 + 1/2 + 2*-3 + 5.0 (a) int -5 (b) double -4.5 (c) int -4 (d) double -5.0 (e) None of the above.
double -5.0 Reason: The execution goes on like this: -4 + 1/2 + 2*-3 + 5.0; -4 + 0 + -6 + 5.0; // integer division: 1/2 truncates .5 -10 + 5.0; // higher type is double 5.0, so -10 is casted to double -5.0; finally, double -5.0.is the answer
To prevent any method from overriding, we declare the method as, (a) static (b) const (c) final (d) abstract (e) none of the above.
final Reason: Final methods of the base class cannot be overridden in the derived Class.
Which of the following variable declaration would NOT compile in a java program? (a) int var; (b) int VAR; (c) int var1; (d) int var_1; (e) int 1_var;.
int 1_var;. Reason: The first character of a variable name should not be a digit.
Multiple inheritance means, (a) one class inheriting from more super classes (b) more classes inheriting from one super class (c) more classes inheriting from more super classes (d) None of the above (e) (a) and (b) above.
one class inheriting from more super classes Reason: Multiple inheritance means one class inheriting from more super classes.