-
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, C]
Explanation :
The Java Virtual Machine calls and executes the main method.
class Course {
String courseName;
}
class EJavaGuru {
public static void main(String args[]) {
Course c = new Course();
c.courseName = "Java";
System.out.println(c.courseName);
}
}
Which of the following statements will be true if the variable courseName is defined as a private variable?
Correct Answers :
[C]
Explanation :
If the variable courseName is defined as a private member, it won’t be accessible from the class EJavaGuru. An attempt to do so will cause it to fail at compile time. Because the code won’t compile, it can’t execute.
package com.ejavaguru.courses;
class Course {
public String courseName;
}
what’s the output of the following code?
package com.ejavaguru;
import com.ejavaguru.courses.Course;
class EJavaGuru {
public static void main(String args[]) {
Course c = new Course();
c.courseName = "Java";
System.out.println(c.courseName);
}
}
Correct Answers :
[C]
Explanation :
The class will fail to compile because a non-public class cannot be accessed outside a package in which it is defined. The class Course therefore can't be accessed from within the class EJavaGuru, even if it is explicitly imported into it. If the class itself isn’t accessible, there’s no point in accessing a public member of a class.
--------------- should be used to store a count of cars manufactured by a car manufacturing company. ------------- should be used to store whether this car manufacturing company modifies the interiors on the customer's request. ------------- should be used to store the maximum speed of a car.
Correct Answers :
[A, D]
Explanation :
Options (a) and (d) are correct. Use a long data type to store big number values, a
boolean data type to store yes/no values as true/false, and a double or float to
store decimal numbers.
Option (b) is incorrect. You can’t use an int to store yes/no or true/false values.
Option (c) is incorrect. You can’t use a char data type to store very long values. Also,
it's conceptually incorrect to track counts using the char data type.
Correct Answers :
[C, D, F, G]
Explanation :
Options (a) and (b) are incorrect. There are no primitive data types in Java with
the names bit and integer. The correct names are byte and int.
Option (c) is correct. It assigns a hexadecimal literal value to the variable a3.
Option (d) is correct. It assigns an octal literal value to the variable a4.
Option (e) is incorrect. It defines a variable of type double, which is used to store
decimal numbers, not integers.
Option (f) is correct. -0 is a valid literal value.
Option (g) is correct. 123456789 is a valid integer literal value that can be assigned
to a variable of type long.
public class IncrementNum {
public static void main(String[] args) {
int ctr = 50;
// INSERT CODE HERE
System.out.println(ctr % 20);
}
}
Correct Answers :
[A, C]
Explanation :
To output a value of 11, the value of the variable ctr should be 51
because 51%20 is 11. Operator % outputs the remainder from a division operation.
The
current value of the variable ctr is 50. It can be incremented by 1 using the correct
assignment or increment operator.
Option (b) is incorrect. Java does not define a =+ operator. The correct operator is +=.
Option (d) is incorrect because it’s assigning a value of 1 to the variable result, not
incrementing it by 1.
public String eJava(int age, String name, double duration)
Correct Answers :
[C, D, E, F, G]
Explanation :
Option (a) is incorrect. Overloaded methods can change the access
modifiers, but changing the access modifier alone won’t make it an overloaded
method. This option also changes the names of the method parameters, but that
doesn’t make any difference to a method signature.
Option (b) is incorrect. Overloaded methods can change the return type of the
method, but changing the return type won’t make it an overloaded method.
Option (c) is correct. Changing the placement of the types of the method parameters
overloads it.
Option (d) is correct. Changing the return type of a method and the placement of
the types of the method parameters overloads it.
Option (e) is correct. Changing the return type of a method and making a change
in the parameter list overload it.
Option (f) is correct. Changing the return type of a method and making a change
in the parameter list overload it.
Option (g) is correct. Changing the parameter list also overloads a method.
class Course {
void enroll(long duration) {
System.out.println("long");
}
void enroll(int duration) {
System.out.println("int");
}
void enroll(String s) {
System.out.println("String");
}
void enroll(Object o) {
System.out.println("Object");
}
}
what is the output of the following code?
class EJavaGuru {
public static void main(String args[]) {
Course course = new Course();
char c = 10;
course.enroll(c);
course.enroll("Object");
}
}
Correct Answers :
[C]
Explanation :
No compilation issues exist with the code. You can overload methods by
changing the type of the method arguments in the list. Using method arguments with
data types having a base-derived class relationship (Object and String classes) is
acceptable. Using method arguments with data types for which one can be automatically
converted to the other (int and long) is also acceptable.
When the code executes course.enroll(c), char can be passed to two overloaded
enroll methods that accept int and long.
The char gets expanded to its nearest
type—int—so course.enroll(c) calls the overloaded method that accepts int,
printing int. The code course.enroll("Object") is passed a String value. Although
String is also an Object, this method calls the specific (not general) type of the argument
passed to it. So course.enroll("Object") calls the overloaded method that
accepts String, printing String.
class EJava { public EJava() {
this(7);
System.out.println("public");
}
private EJava(int val) {
this("Sunday");
System.out.println("private");
}
protected EJava(String val) {
System.out.println("protected");
}
}
class TestEJava {
public static void main(String[] args) {
EJava eJava = new EJava();
}
}
Correct Answers :
[A, D]
Explanation :
You can define overloaded constructors with different access modifiers
in the same way that you define overloaded methods with different access modifiers.
But a change in only the access modifier can’t be used to define overloaded methods
or constructors. private methods and constructors are also counted as overloaded
methods.
The following line of code calls EJava’s constructor, which doesn't accept any
method argument:
EJava eJava = new EJava();
The no-argument constructor of this class calls the constructor that accepts an int
argument, which in turn calls the constructor with the String argument. Because the
constructor with the String constructor doesn't call any other methods, it prints
protected and returns control to the constructor that accepts an int argument. This
constructor prints private and returns control back to the constructor that doesn't
accept any method argument. This constructor prints public and returns control to
the main method.
Correct Answers :
[D, E]
Explanation :
Option (a) is incorrect. By default, an ArrayList creates an array with
an initial size of 10 to store its elements.
Option (b) is incorrect. Starting with Java 7, switch also accepts variables of type
String. Because a String can be stored in an ArrayList, you can use elements of an
ArrayList in a switch construct.
Option (c) is incorrect. Only remove() will remove all elements of an ArrayList.
Option (d) is correct. An ArrayList internally uses an array to store all its elements.
Whenever you add an element to an ArrayList, it checks whether the array
can accommodate the new value. If it can’t, ArrayList creates a larger array, copies
all the existing values to the new array, and then adds the new value at the end of
the array. If you frequently add elements to an ArrayList, it makes sense to create
an ArrayList with a bigger capacity because the previous process isn’t repeated for
each ArrayList insertion.
Option (e) is correct. Calling clone() on an ArrayList will create a separate reference
variable that stores the same number of elements as the ArrayList to be cloned.
But each individual ArrayList element will refer to the same object; that is, the individual
ArrayList elements aren’t cloned.
Correct Answers :
[A, B, C, E]
Explanation :
Option (a) is correct. A developer may prefer using an ArrayList over
an array because it offers all the benefits of an array and a list. For example, you can
easily add or remove elements from an ArrayList.
Option (b) is correct.
Option (c) is correct. An ArrayList can be easily searched, sorted, and have its values
compared using the methods provided by the Collection framework classes.
Option (d) is incorrect. An array requires you to specify the total number of elements
before you can add any element to it. But you don’t need to specify the total
number of elements that you may add to an ArrayList at any time in your code.
Option (e) is correct.
import java.util.*; // line 1
class EJavaGuruArrayList { // line 2
public static void main(String args[]) { // line 3
ArrayList<String> ejg = new ArrayList<>(); // line 4
ejg.add("One"); // line 5
ejg.add("Two"); // line 6
System.out.println(ejg.contains(new String("One"))); // line 7
System.out.println(ejg.indexOf("Two")); // line 8
ejg.clear(); // line 9
System.out.println(ejg); // line 10
System.out.println(ejg.get(1)); // line 11
} // line 12
} // line 13
Correct Answers :
[A, D, E, H, J]
Explanation :
Line 7: The method contains accepts an object and compares it with
the values stored in the list. It returns true if the method finds a match and false
otherwise. This method uses the equals method defined by the object stored in the
list. In the example, the ArrayList stores objects of class String, which has overridden
the equals method. The equals method of the String class compares the values
stored by it. This is why line 7 returns the value true.
Line 8: indexOf returns the index position of an element if a match is found;
otherwise, it returns -1. This method also uses the equals method behind the scenes
to compare the values in an ArrayList. Because the equals method in class String
compares its values and not the reference variables, the indexOf method finds a match
in position 1.
Line 9: The clear method removes all the individual elements of an ArrayList
such that an attempt to access any of the earlier ArrayList elements will throw a runtime
exception. It doesn’t set the ArrayList reference variable to null.
Line 10: ArrayList has overridden the toString method such that it returns a list
of all its elements enclosed within square brackets. To print each element, the
toString method is called to retrieve its String representation.
Line 11: The clear method removes all the elements of an ArrayList. An attempt
to access the (nonexistent) ArrayList element throws a runtime IndexOutOfBounds-
Exception exception.
This question tests your understanding of ArrayList and determining the equality
of String objects.
class EJavaGuru3 {
public static void main(String args[]) {
byte foo = 120;
switch (foo) {
default: System.out.println("ejavaguru"); break;
case 2: System.out.println("e"); break;
case 120: System.out.println("ejava");
case 121: System.out.println("enum");
case 127: System.out.println("guru"); break;
}
}
}
Correct Answers :
[A]
Explanation :
For a switch case construct, control enters the case labels when a
matching case is found. The control then falls through the remaining case labels
until it's terminated by a break statement. The control exits the switch construct
when it encounters a break statement or it reaches the end of the switch construct.
In this question, a matching label is found for case label 120. The control executes
the statement for this case label and prints ejava to the console. Because a break
statement doesn’t terminate the case label, the control falls through to case label 121.
The control executes the statement for this case label and prints enum to the console.
Because a break statement also doesn’t terminate this case label, the control falls
through to case label 127. The control executes the statement for this case label and
prints guru to the console. This case label is terminated by a break statement, so the
control exits the switch construct.
class EJavaGuru4 {
public static void main(String args[]) {
boolean myVal = false;
if (myVal=true)
for (int i = 0; i < 2; i++) System.out.println(i);
else System.out.println("else");
}
}
Correct Answers :
[C]
Explanation :
The expression used in the if construct isn't comparing the
value of the variable myVal with the literal value true—it's assigning the literal value
true to it. The assignment operator (=) assigns the literal value. The comparison
operator (==) is used to compare values.
Because the resulting value is a boolean
value, the compiler doesn't complain about the assignment in the if construct.
The for loop is part of
the if construct, which prints 0 and 1. The else part doesn’t execute because the if
condition evaluates to true. The code has no compilation errors.
class EJavaGuru5 {
public static void main(String args[]) {
int i = 0;
for (; i < 2; i=i+5) {
if (i < 5) continue;
System.out.println(i);
}
System.out.println(i);
}
}
Correct Answers :
[F]
Explanation :
First of all, the following line of code has no compilation errors:
for (; i < 2; i=i+5) {
Using the initialization block is optional in a for loop. In this case, using a semicolon
(;) terminates it.
For the first for iteration, the variable i has a value of 0. Because this value is
less than 2, the following if construct evaluates to true and the continue statement
executes:
if (i < 5) continue;
Because the continue statement ignores all of the remaining statements in a for loop
iteration, the control doesn’t print the value of the variable i, which leads the control
to move on to the next for iteration. In the next for iteration, the value of the variable
i is 5. The for loop condition evaluates to false and the control moves out of
the for loop. After the for loop, the code prints out the value of the variable i, which
increments once using the code i=i+5.
Correct Answers :
[A, B]
Explanation :
Option (a) is correct. Inheritance can allow you to reuse existing code
by extending a class. In this way, the functionality that is already defined in the base
class need not be defined in the derived class. The functionality offered by the base class
can be accessed in the derived class as if it were defined in the derived class.
Option (b) is correct. Common code can be placed in the base class, which can be
extended by all the derived classes. If any changes need to be made to this common
code, it can be modified in the base class. The modified code will be accessible to all
the derived classes.
Option (c) is incorrect. Polymorphism doesn’t pass any special instructions to the
compiler to make the Java code execute on multiple platforms. Java code can execute
on multiple platforms because the Java compiler compiles to virtual machine code,
which is platform-neutral. Different platforms implement this virtual machine.
Option (d) is incorrect. Polymorphic methods can throw exceptions.
void orbit() {}
}
class Moon extends Satellite {
void orbit() {}
}
class ArtificialSatellite extends Satellite {
void orbit() {}
}
Correct Answers :
[A]
Explanation :
All of these options define classes. When methods with the same method signature are defined in classes that share an inheritance relationship, the methods are considered polymorphic.
class Person {}
class Employee extends Person {}
class Doctor extends Person {}
Correct Answers :
[D]
Explanation :
The given code does not define any method in the class Person that is redefined or implemented in the classes Employee and Doctor. Though the classes Employee and Doctor extend the class Person, all these three polymorphism concepts or design principles are based on a method, which is missing in these classes.
Correct Answers :
[C, D]
Explanation :
Option (a) is a true statement. A checked exception is a subclass of java.lang
.Exception, and a runtime exception is a subclass of java.lang.RuntimeException.
java.lang.RuntimeException is a subclass of java.lang.Exception, and java.lang
.Exception is a subclass of java.lang.Throwable. Hence, all the exceptions are subclasses
of java.lang.Throwable.
Option (b) is also a true statement. Unchecked exceptions are subclasses of class
java.lang.RuntimeException, which itself is a subclass of java.lang.Exception.
Hence, a class can be a subclass of class java.lang.Exception and either a checked
or an unchecked exception.
Option (c) is a false statement. Error is not an exception. It does not subclass
java.lang.Exception.
Option (d) is also a false statement. Error need not be part of a method signature,
but checked exceptions must be a part of the method signatures.
class TryFinally {
int tryAgain() {
int a = 10;
try {
++a;
}
finally {
a++;
}
return a;
}
public static void main(String args[]) {
System.out.println(new TryFinally().tryAgain());
}
}
Correct Answers :
[C]
Explanation :
The try block executes, incrementing the value of variable a to 11. This
step is followed by execution of the finally block, which also increments the value of
variable a by 1, to 12. The method tryAgain returns the value 12, which is printed by
the method main.
There are no compilation issues with the code. A try block can be followed by a
finally block, without any catch blocks. Even though the try block doesn’t throw
any exceptions, it compiles successfully. The following is an example of a try-catch
block that won’t compile because it tries to catch a checked exception that's never
thrown by the try block:
try {
++a;
}
catch (java.io.FileNotFoundException e) {
}
|
|
||||||||||||||||||||||||||||