-
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 % - /
// contents of MyClass.java package com.ejavaguru; import java.util.Date; class Student {} class Course {}
Correct Answers :
[B, C]
Explanation :
You can define multiple classes, interfaces, and enums in a Java source
code file.
Option (a) is incorrect. The import statement applies to all the classes, interfaces,
and enums defined within the same Java source code file.
Option (d) is incorrect. If a package statement is defined in the source code
file, all of the classes, interfaces, and enums defined within it will exist in the same
Java package.
class EJavaGuru { public static void main(String[] args) { System.out.println(args[1]+":"+ args[2]+":"+ args[3]); } }what is the output of the previous class, if it is executed using the command:
java EJavaGuru one two three four
Correct Answers :
[D]
Explanation :
The command-line arguments passed to the main method of a class do
not contain the word Java and the name of the class.
Because the position of an array is zero-based, the method argument is assigned
the following values:
args[0] -> one
args[1] -> two
args[2] -> three
args[3] -> four
The class prints two:three:four.
public class EJavaGuru { // INSERT CODE HERE { System.out.println("EJavaGuru"); } }
Correct Answers :
[C]
Explanation :
Option (a) is incorrect. This option defines a valid method but not a
valid main method. The main method should be defined as a static method, which is
missing from the method declaration in option (a).
Option (b) is incorrect. This option is similar to the method defined in option (a),
with one difference. In this option, the square brackets are placed after the name of
the method argument. The main method accepts an array as a method argument, and
to define an array, the square brackets can be placed after either the data type or the
method argument name.
Option (c) is correct. Extra spaces in a class are ignored by the Java compiler.
Option (d) is incorrect. The main method accepts an array of String as a method
argument. The method in this option accepts a single String object.
Option (e) is incorrect. It isn’t a valid method definition and doesn’t specify the
return type of the method. This line of code will not compile.
Correct Answers :
[B,C,D]
Explanation :
Only option (a) is a correct statement. Java primitive data types are predefined by the programming language. They can’t be defined by a programmer.
public class Prim { // line 1 public static void main(String[] args) { // line 2 char a = 'a'; // line 3 char b = -10; // line 4 char c = '1'; // line 5 integer d = 1000; // line 6 System.out.println(++a + b++ * c - d);// line 7 } // line 8 }
Correct Answers :
[A, C, D]
Explanation :
Option (a) is correct. The code at line 4 fails to compile because you can’t assign a
negative value to a primitive char data type without casting.
Option (c) is correct. There is no primitive data type with the name "integer." The
valid data types are int and Integer (a wrapper class with I in uppercase).
Option (d) is correct. The variable d remains undefined on line 7 because its declaration
fails to compile on line 6. So the arithmetic expression (++a + b++ * c - d)
that uses variable d fails to compile. There are no issues with using the variable c of
the char data type in an arithmetic expression. The char data types are internally
stored as unsigned integer values and can be used in arithmetic expressions.
public class Foo { public static void main(String[] args) { int a = 10; long b = 20; short c = 30; System.out.println(++a + b++ * c); } }
Correct Answers :
[A]
Explanation :
The prefix increment operator (++) used with the variable a will increment
its value before it is used in the expression ++a + b++ * c. The postfix increment
operator (++) used with the variable b will increment its value after its initial value is
used in the expression ++a + b++ * c.
Therefore, the expression ++a + b++ * c, evaluates with the following values:
11 + 20 * 30
Because the multiplication operator has a higher precedence than the addition operator,
the values 20 and 30 are multiplied before the result is added to the value 11.
The example expression evaluates as follows:
(++a + b++ * c)
= 11 + 20 * 30
= 11 + 600
= 611
Correct Answers :
[A, B, C, D]
Explanation :
Option (e) is incorrect. There is no constraint on the number of arguments
that can be passed on to a method, regardless of whether the method returns
a value.
Option (f) is incorrect. You can’t return the value null for methods that return
primitive data types. You can return null for methods that return objects (String is a
class and not a primitive data type).
class Person { public String name; public int height; }what is the output of the following code?
class EJavaGuruPassObjects1 { public static void main(String args[]) { Person p = new Person(); p.name = "EJava"; anotherMethod(p); System.out.println(p.name); someMethod(p); System.out.println(p.name); } static void someMethod(Person p) { p.name = "someMethod"; System.out.println(p.name); } static void anotherMethod(Person p) { p = new Person(); p.name = "anotherMethod"; System.out.println(p.name); } }
Correct Answers :
[B]
Explanation :
The class EJavaGuruPassObject1 defines two methods, someMethod and anotherMethod. The method someMethod modifies the value of the object parameter passed to it. Hence, the changes are visible within this method and in the calling method (method main). But the method anotherMethod reassigns the reference variable passed to it. Changes to any of the values of this object are limited to this method. They aren’t reflected in the calling method (the main method).
class EJavaGuruPassPrim { public static void main(String args[]) { int ejg = 10; anotherMethod(ejg); System.out.println(ejg); someMethod(ejg); System.out.println(ejg); } static void someMethod(int val) { ++val; System.out.println(val); } static void anotherMethod(int val) { val = 20; System.out.println(val); } }
Correct Answers :
[C]
Explanation :
When primitive data types are passed to a method, the values of the variables in the calling method remain the same. This behavior doesn't depend on whether the primitive values are reassigned other values or modified by addition, subtraction, or multiplication—or any other operation.
class EJavaGuruArray { public static void main(String args[]) { int[] arr = new int[5]; byte b = 4; char c = 'c'; long longVar = 10; arr[0] = b; arr[1] = c; arr[3] = longVar; System.out.println(arr[0] + arr[1] + arr[2] + arr[3]); } }
Correct Answers :
[E]
Explanation :
The previous code won’t compile due to the following line of code:
arr[3] = longVar;
This line of code tries to assign a value of type long to a variable of type int. Because
Java does support implicit widening conversions for variables, the previous code fails
to compile. Also, the previous code tries to trick you regarding your understanding of
the following:
1) Assigning a char value to an int array element (arr[1] = c)
2) Adding a byte value to an int array element (arr[0] = b)
3) Whether an unassigned int array element is assigned a default value (arr[2])
4) Whether arr[0] + arr[1] + arr[2] + arr[3] prints the sum of all these values,
or a concatenated value.
class EJavaGuruArray2 { public static void main(String args[]) { int[] arr1; int[] arr2 = new int[3]; char[] arr3 = {'a', 'b'}; arr1 = arr2; arr1 = arr3; System.out.println(arr1[0] + ":" + arr1[1]); } }
Correct Answers :
[E]
Explanation :
Because a char value can be assigned to an int value, you might assume that a char array can be assigned to an int array. But we’re talking about arrays of int and char primitives, which aren’t the same as a primitive int or char. Arrays themselves are reference variables, which refer to a collection of objects of similar type.
Correct Answers :
[A, D]
Explanation :
Option (b) is incorrect. This line of code won't compile because new
array() isn't valid code. Unlike objects of other classes, an array isn't initialized using
the keyword new followed by the word array. When the keyword new is used to initialize
an array, it’s followed by the type of the array, not the word array.
Option (c) is incorrect.
To initialize a two-dimensional array, all of these values
must be enclosed within another pair of curly braces, as shown in option (a).
int a = 10; if (a++ > 10) { System.out.println("true"); } { System.out.println("false"); } System.out.println("ABC");
Correct Answers :
[B]
Explanation :
The code has no compilation errors.
{
System.out.println("false");
}
The value false will print no matter what, regardless of whether the condition
in the if construct evaluates to true or false.
Because the opening and closing braces for this code snippet are placed right after
the if construct, it leads us to believe that this code snippet is the else part of the if
construct. Also, note that an if construct uses the keyword else to define the else
part. This keyword is missing in this question.
The if condition (that is, a++ > 10) evaluates to false because the postfix increment
operator (a++) increments the value of the variable a immediately after its earlier
value is used. 10 isn’t greater than 10 so this condition evaluates to false.
class EJavaGuru {
public static void main(String args[]) {
int num = 20;
final int num2;
num2 = 20;
switch (num) {
default: System.out.println("default");
case num2: System.out.println(4);
break;
}
}
}
Correct Answers :
[D]
Explanation :
The code will fail to compile. The case labels require compile-time constant values, and the variable num2 doesn’t qualify as such. Although the variable num2 is defined as a final variable, it isn’t assigned a value with its declaration. The code assigns a literal value 20 to this variable after its declaration, but it isn’t considered to be a compile-time constant by the Java compiler.
class EJavaGuru {
public static void main(String args[]) {
int num = 120;
switch (num) {
default: System.out.println("default");
case 0: System.out.println("case1");
case 10*2-20: System.out.println("case2");
break;
}
}
}
Correct Answers :
[D]
Explanation :
The expressions used for both case labels—that is, 0 and 10*2-20—evaluate to the constant value 0. Because you can’t define duplicate case labels for the switch statement, the code will fail to compile with an error message that states that the code defines a duplicate case label.
class Animal { void jump() { System.out.println("Animal"); } } class Cat extends Animal { void jump(int a) { System.out.println("Cat"); } } class Rabbit extends Animal { void jump() { System.out.println("Rabbit"); } } class Circus { public static void main(String args[]) { Animal cat = new Cat(); Rabbit rabbit = new Rabbit(); cat.jump(); rabbit.jump(); } }
Correct Answers :
[A]
Explanation :
Although the classes Cat and Rabbit seem to override the method jump,
the class Cat doesn’t override the method jump() defined in the class Animal. The
class Cat defines a method parameter with the method jump, which makes it an overloaded
method, not an overridden method. Because the class Cat extends the class
Animal, it has access to the following two overloaded jump methods:
void jump() { System.out.println("Animal"); }
void jump(int a) { System.out.println("Cat"); }
The following line of code creates an object of class Cat and assigns it to a variable of
type Animal:
Animal cat = new Cat();
When you call the method jump on the previous object, it executes the method jump,
which doesn’t accept any method parameters, printing the following value:
Animal
The following code will also print Animal and not Cat:
Cat cat = new Cat();
cat.jump();
class Flower { public void fragrance() {System.out.println("Flower"); } } class Rose { public void fragrance() {System.out.println("Rose"); } } class Lily { public void fragrance() {System.out.println("Lily"); } } class Bouquet { public void arrangeFlowers() { Flower f1 = new Rose(); Flower f2 = new Lily(); f1.fragrance(); } }
Correct Answers :
[D]
Explanation :
Although the code seems to implement polymorphism using classes,
note that neither of the classes Rose or Lily extends the class Flower. Hence, a variable
of type Flower can’t be used to store objects of the classes Rose or Lily. The following
lines of code will fail to compile:
Flower f1 = new Rose();
Flower f2 = new Lily();
Correct Answers :
[B]
Explanation :
Option (a) is incorrect. To implement polymorphism with classes, either an abstract
class or a concrete class can be used as a base class.
Option (c) is incorrect. First of all, no code execution takes place at compile time.
Code can only execute at runtime. In polymorphism, the determination of the exact
method to execute is deferred until runtime and is determined by the exact type of
the object on which a method needs to be called.
class EJava { void method() { try { infibee(); return; } finally { System.out.println("finally 1"); } } void infibee() { System.out.println("infibee"); throw new StackOverflowError(); } public static void main(String args[]) { EJava var = new EJava(); var.method(); } }
Correct Answers :
[B]
Explanation :
No compilation errors exist with the code.
The method infibee throws StackOverflowError, which is not a checked exception.
Even though your code should not throw an error, it is possible syntactically. Your
code will compile successfully.
The call to the method infibee is immediately followed by the keyword return,
which is supposed to end the execution of the method method. But the call to infibee is
placed within a try-catch block, with a finally block. Because infibee doesn’t handle
the error StackOverflowError itself, the control looks for the exception handler in the
method method. This calling method doesn’t handle this error, but defines a finally
block.
The control then executes the finally block. Because the code can’t find an
appropriate handler to handle this error, it propagates to the JVM, which abruptly
halts the code.
Correct Answers :
[B]
Explanation :
No direct relationship exists between exception handling and improved execution of code. Code that handles all the checked exceptions can throw unchecked exceptions and vice versa.
|
|
||||||||||||||||||||||||||||