-
General Knowledge
-
General Topics
- Abbreviations
- Books and Authors
- Famous Academies and Institutes
- First in India (Man)
- First in India (Women)
- Important Dates
- Famous Personalities
- Public Administration Science
- Astronomy
- Biology
- Botany
- Chemistry
- Physics
- Zoology
- Inventions and Scientists Geography
- Geographical Epithets India
- Geographical Epithets World
- Places Associated with Industries in India
- National Parks and Sanctuaries
- Towns on River Banks History
- Indian History and Culture
- Indian National Movement
- Indian Polity and Constitution
- Ancient Period in Indian History
- World History
- Governor General of India Culture
- Languages
- Indian Dance and Music
- Classical Dances of India
- Folk Dances in India and Tribal Dances in India
- Famous Dancers, Instrumentalists, Vocalists in India World
- First in the World
- Parliament Names
- United Nation Organizations (UNO)
- World's Famous News Agencies
- World Industries
- National Emblems
- Countries and Alternative Names
- Countries and Capitals
- View More topics...
- General Aptitude
- Problems on Ages
- Alligation and Mixture
- Area
- Arithmetic Progression
- Average
- Banker's Discount
- Boats and Streams
- Calendar
- Chain Rule
- Clock
- Compound Interest
- Decimal Fraction
- Height and Distance
- Logarithms
- Mensurations
- Numbers
- Odd Man Out and Series
- Partnership and Share
- Percentage
- Permutation and Combination
- Pipes and Cisterns
- Probability
- Problems on H.C.F and L.C.M
- Problems on Numbers
- Problems on Trains
- Profit and Loss
- Races and Games
- Ratio and Proportion
- Simple Interest
- Simplification
- Stocks and Shares
- Surds and Indices
- Time and Distance
- Time and Work
- True Discount
- Volume & Surface Areas
- General English
- Antonyms
- Synonyms
- Vocabulary Test
- One Word Substitution
- Sentence Completion
- Sentence Improvement
- Idioms & Phrases
- Homonyms
- Word Formation
- Active & Passive Voice
- Direct and Indirect Speech
- Spotting Errors
- Double Synonyms
- Choose the Appropriate Filter
- Spelling Test
- Transformation
- Reconstruction of Sentence
- Chooose the Correct or Incorrect Sentence
- Networking
- Interview Questions
-
Programming
- .NET
- Java
- ASP.NET
- C++
- Perl
- Python
- Ruby and Rails
- Struts
- Core Java
- Hibernate Database
- DB2
- MS SQL Server
- MySQL
- Oracle
- SQL
- DBMS
- Data Warehousing
- Data structures and Algorithms Cisco
- CCNA
- CCNP Routing
- CCNP Switching
- Internetworking
- Border Gateway Protocol Windows
- MCSE
- Exchange Server
- Windows Server 2008
- DNS & Active Directory
- Firewall Questions Linux
- Unix
- Linux Server Administrator
- Linux System Administrator
- Linux File Manipulation
- Database
- Home
- Online-Quiz
- Programming
- Java Online Quiz
Instructions
- Total Questions 20
- Each question carry 1 mark
- Must answer all the questions (otherwise report card will not be generated)
- If you dont want to take a test, simply click the check answers button and view all the answers with explanations
- Do Not Refresh the Page
- No Time Limit
- Good Luck :)
You Scored % - /
Correct Answers :
[A, B, C, D]
Explanation :
The Java compiler and Java Runtime Environment aren’t components of a Java source file.
- comments
- import statement
- package statement
- methods
- class declaration
- variables
Correct Answers :
[A,B,D]
Explanation :
The comments can appear anywhere in a class. They can appear before
and after package and import statements. They can appear before or after a class,
method, or variable declaration.
The first statement (if present) in a class should be a package statement. It can’t be
placed after an import statement or a declaration of a class.
The import statement should follow a package statement and be followed by a
class declaration.
The class declaration follows the import statements, if present. It’s followed by the
declaration of the methods and variables.
Answer (c) is incorrect. None of the variables or methods can be defined before the
definition of a class or interface.
Correct Answers :
[D]
Explanation :
Answer (a) is incorrect because #connect isn’t a statement in Java. # is
used to add comments in UNIX.
Option (b) is incorrect because a package name (Java compiler) cannot contain
spaces. Also, java virtual machine isn’t a valid package name to be imported in a
class. The package name to be imported cannot contain spaces.
Option (c) is incorrect because a package statement should be placed before an
import statement.
Option (e) is incorrect. #package and $import aren’t valid statements or directives
in Java.
Option (f) is incorrect. Java is case-sensitive, so the word class is not the same as
the word Class. The correct keyword to define a class is class.
boolean myBool = false; // line 1 int yourInt = 10; // line 2 float hisFloat = 19.54f; // line 3 System.out.println(hisFloat > yourInt); // line 4 System.out.println(yourInt = 10); // line 5 System.out.println(myBool > false); // line 6
Correct Answers :
[B]
Explanation :
Because the question mentioned swapping the functionality of the operator
> with =, the code on lines 4, 5, and 6 will actually evaluate to the following:
System.out.println(hisFloat = yourInt);
System.out.println(yourInt > 10);
System.out.println(myBool = false);
The result is shown in b.
Note that the expression myBool = false uses the assignment operator (=) and not
a comparison operator (==). This expression assigns boolean literal false to myBool;
it doesn’t compare false with myBool. Watch out for similar (trick) assignments in the
exam, which may seem to be comparing values.
public class Prim { // line 1 public static void main(String[] args) { // line 2 int num1 = 12; // line 3 float num2 = 17.8f; // line 4 boolean eJavaResult = true; // line 5 boolean returnVal = num1 >= 12 && num2 < 4.567 // line 6 || eJavaResult == true; System.out.println(returnVal); // line 7 } // line 8 }
Correct Answers :
[B, C]
Explanation :
Option (a) is incorrect because the code prints true.
Option (d) is incorrect because the code prints false.
Both the short-circuit operators && and || have the same operator precedence. In
the absence of any parentheses, they are evaluated from left to right. The first expression,
num1 >= 12, evaluates to true. The && operator evaluates the second operand
only if the first evaluates to true. Because && returns true for its first operand, it evaluates
the second operand, which is (num2 < 4.567 || eJavaResult == true). The second
operand evaluates to true; hence the variable returnVal is assigned true.
boolean b = false; int i = 90; System.out.println(i >= b);
Correct Answers :
[D]
Explanation :
The code will fail to compile; hence, it can’t execute. You can’t compare incomparable types, such as a boolean value with a number.
Correct Answers :
[A, B]
Explanation :
A well-encapsulated class should be like a capsule, hiding its instance variables from the outside world. The only way you should access and modify instance variables is through the public methods of a class to ensure that the outside world can access only the variables the class allows it to. By defining methods to assign values to its instance variables, a class can control the range of values that can be assigned to them.
public class Person { public int height; public void setHeight(int newHeight) { if (newHeight <= 300) height = newHeight; } }
Correct Answers :
[D]
Explanation :
This class isn't well encapsulated because its instance variable height is defined as a public member. Because the instance variable can be directly accessed by other classes, the variable doesn’t always use the method setHeight to set its height. The class Person can’t control the values that can be assigned to its public variable height.
Correct Answers :
[B and D]
Explanation :
Option (a) is incorrect. The question specifies the method should return a
decimal number (type double or float), but this method doesn’t return any value.
Option (b) is correct. This method accepts three integer values: byte, int, and
int. It computes the sum of these integer values and returns it as a decimal number
(data type double). Note that the name of the method is subtractNumbers, which
doesn’t make it an invalid option. Practically, one wouldn't name a method subtract-
Numbers if it's adding them. But syntactically and technically, this option meets the
question’s requirements and is a correct option.
Option (c) is incorrect. This method doesn’t accept integers as the method arguments.
The type of the method argument arg3 is double, which isn’t an integer.
Option (d) is correct. Even though the name of the method seems weird, it
accepts the correct argument list (all integers) and returns the result in the correct
data type (float).
class EJavaGuruStringBuilder2 { public static void main(String args[]) { StringBuilder sb1 = new StringBuilder("123456"); sb1.subSequence(2, 4); sb1.deleteCharAt(3); sb1.reverse(); System.out.println(sb1); } }
Correct Answers :
[C]
Explanation :
Like the method substring, the method subSequence doesn’t modify
the contents of a StringBuilder. Hence, the value of the variable sb1 remains
123456, even after the execution of the following line of code:
sb1.subSequence(2, 4);
The method deleteCharAt deletes a char value at position 3. Because the positions
are zero-based, the digit 4 is deleted from the value 123456, resulting in 12356. The
method reverse modifies the value of a StringBuilder by assigning to it the reverse
representation of its value. The reverse of 12356 is 65321.
class EJavaGuruStringBuilder { public static void main(String args[]) { StringBuilder ejg = new StringBuilder(10 + 2 + "SUN" + 4 + 5); ejg.append(ejg.delete(3, 6)); System.out.println(ejg); } }
Correct Answers :
[A]
Explanation :
This question tests you on your understanding of operators, String, and
StringBuilder. The following line of code returns 12SUN45:
10 + 2 + "SUN" + 4 + 5
The + operator adds two numbers but concatenates the last two numbers. When the
+ operator encounters a String object, it treats all the remaining operands as
String objects.
Unlike the String objects, StringBuilder objects are mutable. The append and
delete methods defined in this class change its value. ejg.delete(3, 6) modifies the
existing value of the StringBuilder to 12S5. It then appends the same value to itself
when calling ejg.append(), resulting in the value 12S512S5.
class EJavaGuruString2 { public static void main(String args[]) { String ejg = "game"; ejg.replace('a', 'Z').trim().concat("Aa"); ejg.substring(0, 2); System.out.println(ejg); } }
Correct Answers :
[E]
Explanation :
String objects are immutable. It doesn’t matter how many methods you execute on a String object; its value won’t change. Variable ejg is initialized with the String value "game". This value won’t change, and the code prints game.
class Loop2 { public static void main(String[] args) { int i = 10; do while (i < 15) i = i + 20; while (i < 2); System.out.println(i); } }
Correct Answers :
[B]
Explanation :
The condition specified in the do-while loop evaluates to false (because 10 <2 evaluates to false). But the control enters the do-while loop because the dowhile loop executes at least once—its condition is checked at the end of the loop. The while evaluates to true for the first iteration and adds 20 to i, making it 30. The while loop doesn’t execute for the second time. Hence, the value of the variable i at the end of the execution of the previous code is 30.
class Loop2 { public static void main(String[] args) { int i = 10; do while (i++ < 15) i = i + 20; while (i < 2); System.out.println(i); } }
Correct Answers :
[D]
Explanation :
This question uses a postfix unary operator in the
while condition.
The condition specified in the do-while loop evaluates to false (because 10<2
evaluates to false). But the control enters the do-while loop because the do-while
loop executes at least once—its condition is checked at the end of the loop. This question
prints outs 32, not 30, because the condition specified in the while loop (which
has an increment operator) executes twice.
In this question, the while loop condition executes twice. For the first evaluation,
i++ < 15 (that is, 10<15) returns true and increments the value of variable i by 1 (due
to the postfix increment operator). The loop body modifies the value of i to 31.
The second condition evaluates i++<15 (that is, 31<15) to false. But due to the postfix increment operator value of i, it increments to 32. The final value is printed as 32.
Correct Answers :
[C]
Explanation :
The enhanced for loop can be used within all types of looping and conditional constructs. Notice the use of “can” and “can’t” in the answer options. It’s important to take note of these subtle differences.
class Base { String var = "EJava"; void printVar() { System.out.println(var); } } class Derived extends Base { String var = "Infibee"; void printVar() { System.out.println(var); } } class QReference { public static void main(String[] args) { Base base = new Base(); Base derived = new Derived(); System.out.println(base.var); System.out.println(derived.var); base.printVar(); derived.printVar(); } }
Correct Answers :
[A]
Explanation :
With inheritance, the instance variables bind at compile time and the
methods bind at runtime. The following line of code refers to an object of the class
Base, using a reference variable of type Base. Hence, both of the following lines of
code print EJava:
System.out.println(base.var);
base.printVar();
But the following line of code refers to an object of the class Derived using a reference
variable of type Base:
Base derived = new Derived();
Because the instance variables bind at compile time, the following line of code
accesses and prints the value of the instance variable defined in the class Base:
System.out.println(derived.var); // prints EJava
In derived.printVar(), even though the method printVar is called using a reference
of type Base, the JVM is aware that the method is invoked on a Derived object
and so executes the overridden printVar method in the class Derived.
Line1> interface Employee {} Line2> interface Printable extends Employee { Line3> String print(); Line4> } Line5> class Programmer { Line6> String print() { return("Programmer - Mala Gupta"); } Line7> } Line8> class Author extends Programmer implements Printable, Employee { Line9> String print() { return("Author - Mala Gupta"); } Line10> }
Correct Answers :
[C]
Explanation :
The methods in an interface are implicitly public. A non-abstract class
that implements an interface must implement all the methods defined in the interface.
While overriding or implementing the methods, the accessibility of the implemented
method must be public. An overriding method can’t be assigned a weaker
access privilege than public.
Option (a) is incorrect. There are no issues with the interface Printable extending
the interface Employee and the class Author implementing both of these interfaces.
Option (b) is incorrect. Adding the access modifier to the method print on line 3
will not make any difference to the existing code. The methods defined in an interface
are implicitly public.
Option (d) is incorrect. There are no issues with a class implementing two interfaces
when one of the interfaces extends the other interface.
class Course { String courseName; Course() { Course c = new Course(); c.courseName = "Oracle"; } } class EJavaGuruPrivate2 { public static void main(String args[]) { Course c = new Course(); c.courseName = "Java"; System.out.println(c.courseName); } }
Correct Answers :
[D]
Explanation :
This class will throw StackOverflowError at runtime. The easiest way to look for a StackOverflowError is to locate recursive method calls. In the question’s code, the constructor of the class Course creates an object of the class Course, which will call the constructor again. Hence, this becomes a recursive call and ends up throwing StackOverflowError at runtime. (As you know, an exception or an error can be thrown only at runtime, not compile time.)
Correct Answers :
[B, C, D]
Explanation :
Option (a) is incorrect. You can handle runtime exceptions the way you
can handle a checked exception in your code: using a try-catch block.
Option (b) is correct. You shouldn’t try to handle errors in your code. Or, to put it
another way, you can’t do much when an error is thrown by your code. Instead of trying
to handle errors in your code, you should resolve the code that results in these
errors. For example, StackOverflowError is an error that will be thrown by your code
if your code executes a method recursively without any exit condition. This repetition
will consume all the space on the stack and result in a StackOverflowError.
Option (c) is correct. If you fail to implement either of these options, your code
won’t compile.
Option (d) is correct. It isn’t mandatory for runtime exceptions to be included in
a method’s throws clause. Usually this inclusion is unnecessary, but if you do include
it, your code will execute without any issues.
Option (e) is incorrect. Runtime exception and all its subclasses are not checked
exceptions.
class EJavaGuruExcep2 { public static void main(String args[]) { EJavaGuruExcep2 var = new EJavaGuruExcep2(); var.printArrValues(args); } void printArrValues(String[] arr) { try { System.out.println(arr[0] + ":" + arr[1]); } catch (NullPointerException e) { System.out.println("NullPointerException"); } catch (IndexOutOfBoundsException e) { System.out.println("IndexOutOfBoundsException"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException"); } } }
Correct Answers :
[D]
Explanation :
The key to answering this question is to be aware of the following two facts:
1) Exceptions are classes. If an exception’s base class is used in a catch block, it
can catch all the exceptions of its derived class. If you try to catch an exception
from its derived class afterward, the code won’t compile.
2) ArrayIndexOutOfBoundsException is a derived class of IndexOutOfBounds-
Exception.
The rest of the points try to trick you into believing that the question is based on the
arguments passed to a main method.
|
|
||||||||||||||||||||||||||||