Essentials Of Core Java

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Learningcorejava
L
Learningcorejava
Community Contributor
Quizzes Created: 2 | Total Attempts: 343
| Attempts: 106 | Questions: 35
Please wait...
Question 1 / 35
0 %
0/100
Score 0/100
1. ______ command is used to invoke Java compiler

Explanation

The correct answer is "javac". The javac command is used to invoke the Java compiler. It is used to compile Java source code files (.java) into bytecode files (.class) that can be executed by the Java Virtual Machine (JVM).

Submit
Please wait...
About This Quiz
Essentials Of Core Java - Quiz


Quiz based on must know concepts of Core Java covering almost all essential aspects of programming language.
Make sure you come out with flying colors :)
Passing: 70%
Time: 30... see moreMinutes see less

2. If there is a public class in a source file, the name of the file must match the name of the public class. True or False?

Explanation

In Java, when there is a public class in a source file, the name of the file must match the name of the public class. This is because the Java compiler expects the source file to have the same name as the public class in order to properly compile and run the code. If the names do not match, it will result in a compilation error. Therefore, the given answer "True" is correct.

Submit
3. Is below code valid?
public void getValue(){
    try{
        System.out.println("Hi");
    }catch(OutOfMemoryError e){
        System.out.println("Error occured");
    }finally{
        System.out.println("Finally always executes");
    }
}

Explanation

The given code is valid because it follows the correct syntax for a method declaration. The method "getValue()" does not have any parameters and has a return type of "void". Inside the method, there is a try-catch-finally block. The try block contains a print statement that will always execute and print "Hi". The catch block is catching an "OutOfMemoryError" exception, but since it is not being thrown in the try block, it will not be executed. Finally, the finally block will always execute and print "Finally always executes". Therefore, the code is valid.

Submit
4. The first concrete subclass of an abstract class must implement all abstract methods of the superclass. True or False?

Explanation

In object-oriented programming, an abstract class is a class that cannot be instantiated and is meant to be subclassed. Abstract methods are declared in the abstract class but do not have an implementation. When a concrete subclass extends an abstract class, it must provide implementations for all the abstract methods defined in the superclass. This is because the concrete subclass is responsible for providing the missing functionality that the abstract methods represent. Therefore, the statement is true.

Submit
5. ______________ keyword indicates that a method can be accessed by only one thread at a time.

Explanation

The keyword "synchronized" indicates that a method can be accessed by only one thread at a time. This means that multiple threads cannot simultaneously execute the synchronized method, ensuring that only one thread can access and modify the shared data at any given time. This helps prevent race conditions and ensures thread safety in concurrent programming.

Submit
6. ____________ Allows code defined in one class to be reused in other classes

Explanation

Inheritance allows code defined in one class to be reused in other classes. It is a fundamental concept in object-oriented programming where a class can inherit properties and methods from another class, known as the parent class or superclass. This allows for code reuse, as the child class can inherit the behavior of the parent class and also add its own unique behavior. Inheritance promotes code organization, modularity, and extensibility.

Submit
7.
CoreJava java = new CoreJava(){
    public void learn(){
        System.out.println("YavaJavaYavaJava");
    }
};
Above code is an example of:

Explanation

The given code is an example of an anonymous inner class. This is because it creates a new class that extends the CoreJava class and overrides its learn() method. The code also creates an instance of this anonymous inner class and calls the learn() method, which prints "YavaJavaYavaJava" to the console.

Submit
8. The generic Type information does not exist at runtime. All generic code is strictly for compiler. This process of removing type information out of the class bytcode is called "Type Erasure". True or False?

Explanation

The statement is true. Generic type information is not available at runtime and is only used by the compiler. The process of removing type information from the class bytecode is called "Type Erasure".

Submit
9. Constructors can be private. True or False?

Explanation

Constructors can be made private in order to restrict the creation of objects of a class to only within the class itself. This can be useful in certain scenarios where the class wants to control the object creation process and prevent it from being instantiated by external code. By making the constructor private, it can only be accessed and used by other methods within the class, ensuring that the object creation is done according to the class's requirements and restrictions.

Submit
10. ___________ method is a mechanism to run some code just before object is deleted by garbage collector.

Explanation

The finalize method is a mechanism in Java that allows code to be executed just before an object is deleted by the garbage collector. This method is called by the garbage collector when it determines that there are no more references to the object. It can be used to perform any necessary cleanup or resource release operations before the object is destroyed.

Submit
11. If you want to save the state of your object, you can implement ________ interface

Explanation

If you want to save the state of your object, you can implement the Serializable interface. Serializable is a marker interface in Java that allows objects to be converted into a byte stream, which can then be saved to a file or sent over a network. By implementing the Serializable interface, the object's state can be persisted and later restored, making it suitable for tasks like object serialization and deserialization.

Submit
12. ___________ modifier indicates that a method is implemented in platform-dependent code

Explanation

The correct answer is "native". In Java, the "native" modifier is used to indicate that a method is implemented in platform-dependent code, typically written in another programming language such as C or C++. This allows the method to interact directly with the underlying system or hardware, providing low-level functionality that may not be available in pure Java code.

Submit
13. Which of the below for loops are valid?

Explanation

The first valid for loop is "for ( ; ; ;){}". This is a valid infinite loop as it does not have any initialization, condition, or iteration expression. It will continue to execute indefinitely unless there is a break or return statement inside the loop.

The second valid for loop is "for (int i = 0; i

Submit
14. _____________ operator can be used to check if object is of particular type

Explanation

The instanceof operator can be used to check if an object is of a particular type. It returns true if the object is an instance of the specified type, and false otherwise.

Submit
15. Which of the following is/are correct statement(s)

Explanation

Instance variables live on the heap because they are associated with an instance of a class and are created when an object is instantiated. Local variables, on the other hand, live on the stack because they are created and destroyed within the scope of a method or block. Objects also live on the heap because they are dynamically allocated and deallocated using the "new" keyword.

Submit
16. What would be the output:
String website = "www.YavaYavaJavaJava.com";
System.out.println(s.substring(4,20));
 

Explanation

The given code snippet creates a string variable "website" with the value "www.YavaYavaJavaJava.com". The next line of code uses the substring method to extract a portion of the string, starting from index 4 and ending at index 20. This will result in the output "YavaYavaJavaJava", which is the substring of the original string that falls within the specified range.

Submit
17. Good Object Oriented design should

Explanation

Good Object Oriented design should have loose coupling but high cohesion. Loose coupling means that the components of the system are not tightly dependent on each other, allowing for easier maintenance, testing, and flexibility. High cohesion means that the components within a module are closely related and focused on a single task or responsibility. This promotes better code organization and readability. Therefore, a design with loose coupling but high cohesion is considered a good practice in Object Oriented design.

Submit
18. A extends B is correct only if..

Explanation

The correct answer is that A extends B is correct only if A is a class and B is a class, or if A is an interface and B is an interface. This is because in object-oriented programming, the "extends" keyword is used to establish an inheritance relationship between classes, where a subclass inherits the properties and behaviors of its superclass. Similarly, interfaces can also extend other interfaces, allowing them to inherit method signatures. Therefore, for the statement "A extends B" to be correct, both A and B must be either classes or interfaces.

Submit
19. If you are marking an instance variable as _________, you're telling the JVM to ignore this variable when you attempt to serialize the object containing it.

Explanation

By marking an instance variable as "transient", you are instructing the JVM to exclude this variable from the serialization process. Serialization is the process of converting an object into a byte stream to store it in memory or transfer it over a network. When an object is serialized, all of its instance variables are also serialized by default. However, by declaring a variable as transient, you are indicating that it should not be serialized along with the object. This is useful when there are certain variables that do not need to be persisted or when their values can be derived or obtained again upon deserialization.

Submit
20. Methods can be overloaded or overriden while constructors can

Explanation

Constructors cannot be overridden because they are not inherited like methods. Each class has its own constructor, and when a subclass is created, the superclass constructor is not automatically called. However, constructors can be overloaded, which means that a class can have multiple constructors with different parameters. Overloading allows for the creation of objects with different initializations based on the parameters passed to the constructor.

Submit
21. assert keyword was introduced in Java version ____

Explanation

The assert keyword was introduced in Java version 1.4.

Submit
22. Which of the following are examples of sorted collection?

Explanation

TreeSet and TreeMap are examples of sorted collections because they both implement the SortedSet and SortedMap interfaces respectively. These interfaces provide methods to maintain the elements in a sorted order based on their natural ordering or a custom comparator. TreeSet stores elements in a sorted order without any duplicates, while TreeMap stores key-value pairs in a sorted order based on the keys. ArrayList and TreeList, on the other hand, are not sorted collections as they do not maintain any specific order for their elements.

Submit
23. Convert below code using ternary operators:
String x = "";
if(i > 10){
    x = "Hello";
}else {
    x = "World"
}

Explanation

The correct answer is "String x = (i > 10) ? "Hello" : "World";". This is the correct conversion of the given code using ternary operators. It checks if the variable "i" is greater than 10, and if it is, assigns the value "Hello" to the string variable "x". If it is not, it assigns the value "World" to "x".

Submit
24. What would be the output of following code:
Class Animal {
    public void eat(){
        System.out.println("Animal is eating");
    }
}

Class Horse extends Animal(){
    public void eat(){
        System.out.println("Horse is eating")
    }
}

public Class Application{
    public static void main(String [] args){
        Animal animal = new Horse();
        animal.eat();
    }
}

Explanation

The output of the code would be "Horse is eating". This is because the variable "animal" is declared as type "Animal" but is assigned a new instance of the "Horse" class. Since the "eat" method is overridden in the "Horse" class, the version of the method in the "Horse" class is called when the "eat" method is invoked on the "animal" object.

Submit
25. Which of the following is/are true statement(s)?

Explanation

The first statement is true because StringBuffer is slower than StringBuilder due to its synchronization feature, which adds overhead to its operations. The second statement is also true as StringBuffer is synchronized, meaning that it is thread-safe and can be accessed by multiple threads simultaneously without causing any data inconsistency issues. The third statement is true as well because strings in Java are immutable, meaning that their values cannot be changed once they are created. The fourth statement is false because the choice between StringBuffer and StringBuilder depends on the specific requirements of the program.

Submit
26. What would be the output of below code:
String string = "Hi";

switch(string.length()){
    case 1:
        System.out.print("One ");
    case 2:
        System.out.print("Two ");
    case 3:
        System.out.print("Three ");
    default:
        System.out.print("Default ");
}

Explanation

The output of the code will be "Two Three Default". This is because the length of the string is 2, so the case 2 block will be executed and "Two " will be printed. Since there are no break statements after each case, the code will continue to execute the following cases. Therefore, the case 3 block will also be executed and "Three " will be printed. Finally, the default block will be executed and "Default " will be printed.

Submit
27. If Horse extends Animal then which of the following is correct?

Explanation

If Horse extends Animal, then it is correct to say that Horse horse = new Horse(), Animal horse = new Animal(), and Animal horse = new Horse(). This is because when a class extends another class, an object of the child class can be assigned to a variable of the parent class. In this case, since Horse extends Animal, a Horse object can be assigned to a variable of type Horse or Animal, and an Animal object can be assigned to a variable of type Animal. However, it is not correct to say Horse horse = new Animal(), as an object of the parent class cannot be assigned to a variable of the child class.

Submit
28. The return type of constructor must always be void. True or False?

Explanation

The return type of a constructor is not specified, not even void. In fact, constructors do not have a return type at all. They are responsible for initializing the object and do not return any value. Therefore, the correct answer is False.

Submit
29. Is it possible to have abstract static method for a class?

Explanation

It is not possible to have an abstract static method for a class because an abstract method is meant to be overridden by the subclasses, but a static method belongs to the class itself and cannot be overridden. Therefore, it is not meaningful to declare a static method as abstract.

Submit
30. Which of the following methods are from Thread class:

Explanation

The methods sleep, yield, and join are from the Thread class. The sleep method is used to pause the execution of a thread for a specified amount of time. The yield method is used to voluntarily give up the current thread's turn to execute and allow other threads to run. The join method is used to wait for a thread to complete its execution before moving on to the next line of code.

Submit
31. Which of the following are checked exceptions?

Explanation

NullPointerException and ArrayIndexOutOfBoundsException are not checked exceptions because they are subclasses of RuntimeException, which is an unchecked exception. However, IllegalStateException and FileNotFoundException are checked exceptions because they are not subclasses of RuntimeException and must be declared in the method signature or handled using try-catch blocks.

Submit
32. How many String objects would be created in following code:
String s1 = "YavaYava";
String s2 = "JavaJava";
String s3 = ".com";
s1 = "www." + s1 + s2 + s3;
String s4 = new String("www.YavaYavaJavaJava.com");

Explanation

In the given code, a total of 6 String objects would be created.

1. The first String object is created when the string "YavaYava" is assigned to the variable s1.
2. The second String object is created when the string "JavaJava" is assigned to the variable s2.
3. The third String object is created when the string ".com" is assigned to the variable s3.
4. The fourth String object is created when the concatenation of "www.", s1, s2, and s3 is assigned to the variable s1.
5. The fifth String object is created when the string "www.YavaYavaJavaJava.com" is passed as an argument to the String constructor, creating a new String object assigned to the variable s4.
6. The sixth String object is created when the concatenation of "www.", s1, s2, and s3 is assigned to the variable s1 again.

Submit
33. ArrayList

Explanation

The correct answer is that ArrayList implements the RandomAccess (marker) interface, is an ordered but unsorted collection, and is faster than vector. The RandomAccess interface is a marker interface that indicates that the implementing class supports fast random access to its elements. ArrayList maintains the order of elements as they are added but does not sort them automatically. It is faster than vector because it is not synchronized, which means it does not incur the overhead of thread-safety. Additionally, ArrayList allows duplicate elements.

Submit
34. Which of the following is/are illegal definition(s) of a method?

Explanation

Both "public abstract getValue(){}" and "public abstract final getValue();" are illegal definitions of a method.

In the first option, "public abstract getValue(){}", the method is declared as abstract, which means it does not have a body. However, it is also defined with curly braces {}, indicating that it should have an implementation. This is contradictory and therefore illegal.

In the second option, "public abstract final getValue();", the method is declared as both abstract and final. These two modifiers are mutually exclusive. Abstract methods are meant to be overridden in subclasses, while final methods cannot be overridden. Therefore, combining them in the same method definition is not allowed.

Submit
35. Which of the following is/are true statements regarding equals method?

Explanation

The first statement is true because if X equals Y and Y equals Z, then transitivity dictates that X must also equal Z.

The second statement is true because the equals method typically returns false when comparing an object to null.

The third statement is also true because calling the equals method on a null object will result in a NullPointerException.

Submit
View My Results

Quiz Review Timeline (Updated): Feb 13, 2024 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Feb 13, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Aug 25, 2014
    Quiz Created by
    Learningcorejava
Cancel
  • All
    All (35)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
______ command is used to invoke Java compiler
If there is a public class in a source file, the name of the file must...
Is below code valid?public void getValue(){ ...
The first concrete subclass of an abstract class must implement all...
______________ keyword indicates that a method can be accessed by only...
____________ Allows code defined in one class to be reused in other...
CoreJava java = new CoreJava(){ ...
The generic Type information does not exist at runtime. All generic...
Constructors can be private. True or False?
___________ method is a mechanism to run some code just before object...
If you want to save the state of your object, you can implement...
___________ modifier indicates that a method is implemented in...
Which of the below for loops are valid?
_____________ operator can be used to check if object is of particular...
Which of the following is/are correct statement(s)
What would be the output:String website =...
Good Object Oriented design should
A extends B is correct only if..
If you are marking an instance variable as _________, you're...
Methods can be overloaded or overriden while constructors can
Assert keyword was introduced in Java version ____
Which of the following are examples of sorted collection?
Convert below code using ternary operators:String x = ""; ...
What would be the output of following code: Class Animal { ...
Which of the following is/are true statement(s)?
What would be the output of below code:String string =...
If Horse extends Animal then which of the following is correct?
The return type of constructor must always be void. True or False?
Is it possible to have abstract static method for a class?
Which of the following methods are from Thread class:
Which of the following are checked exceptions?
How many String objects would be created in following code:String s1 =...
ArrayList
Which of the following is/are illegal definition(s) of a method?
Which of the following is/are true statements regarding equals method?
Alert!

Advertisement