Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

AJAY KUMAR GARG ENGINEERING COLLEGE GHAZIABAD

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Subject Name: OOP’s with JAVA


Subject Code: BCS403
Placement Questions
Q.1 Write the syntax for creating the subclass of a class?
Ans). A subclass can be created by using the “extends” keyword. The syntax for declaring a
subclass of class is as follows:
class subclassName extends superclassName
{
// Variables of subclass
// Methods of subclass
}
where class and extends are two keywords.
Q2.Are constructor and instance initialization block inherited to subclass?
Ans: No, constructor and instance initialization block of the superclass cannot be inherited to its
subclass but they are executed while creating an object of the subclass.

Q3.Why multiple inheritance is not supported in java through class?


Ans: Multiple inheritance means that one class extends two superclasses or base classes but in
Java, one class cannot extend more than one class simultaneously. At most, one class can extend
only one class.
Therefore, to reduce ambiguity, complexity, and confusion, Java does not support multiple
inheritance through classes.
Q4. What is Association in OOPs concept?
Ans: Association in Java is one of the core concepts of OOPs. It establishes the relation between
two classes that are independent of one another.
In association, all objects have their own life cycle and there is no ownership between objects.
Association can be unidirectional or bidirectional.
Q5.What is meant by Composition in OOPs?
Ans: Composition in java is one of the core concepts of OOPs and a more restrictive case of
aggregation. It represents has-a relationship that contains an object and cannot exist on its own.
In simple words, if a class object holds an object of another class, it is called composition. It
establishes strong relationship between two objects than aggregation.

Q6. Can abstract classes in Java implement interfaces? Do they have to implement all methods?
Ans .Yes, abstract classes can implement interfaces using the implements. Since they are
abstract, they are not required to implement all methods. Having an abstract base class and an
interface to declare the type is a recommended practice. An example is an interface
java.util.Listand the corresponding abstract class java.util.AbstractList. Because AbstractListit
implements all common methods, specific implementations (such as LinkedListandArrayList) do
not have to implement all methods, as would be the case if they implemented the interface
Listdirectly. This solution combines the benefit of using an interface to declare a type with the
flexibility of an abstract class to implement all the common behavior in one place.

Q7 .Diffrentiate between abstract class and concrete class?


Ans: There are mainly two differences between an abstract class and concrete class. They are:
a) We cannot create an object of abstract class. Only objects of its non-abstract (or concrete)
sub classes can be created.
b) It can have zero or more abstract methods that are not allowed in a non-abstract class
(concrete class).

Q8.What happens if a class has implemented an interface but has not provided implementation
for that method defined in Interface?
Ans: The class has to be declared with an abstract modifier. This will be enforced by the Java
compiler.
Q9.Why an Interface method cannot be declared as final in Java?
Or, Can a method within an interface be marked as final?
Ans: Not possible. Doing so will result the compilation error problem. This is because a final
method cannot be overridden in java. But an interface method should be implemented by another
class.So, the interface method cannot be declared as final. The modifiers such as public and
abstract are only applicable for method declaration in an interface.
Q 10. Can you explain the purpose of the static keyword in Java?
Ans- The purpose of static keywords in Java is to indicate that a variable, method or inner class
is a class variable or method. Static keywords have an association with the class rather than
representing a specific instance of it. In addition, we can directly assess static keywords or
methods from the class, without creating an instance of the class. In summary, we can define
static keywords in Java as a tool to define utility methods, inner classes and constants that are
independent of the state of an instance of the class.

Q11.Can you create a static inner class in Java?


Ans- Yes, I can create a static inner class in Java. To create this, I first declare the inner class
within the outer class. In the next step, I add the static keyword to the inner class definition. I
create a static inner class in Java to provide additional encapsulation and access to the members
of the outer class. The static inner class in Java can also have its own fields and methods, and I
can use it just like any other class.

Q12. What are lambda expressions in Java, and how do they differ from traditional anonymous
classes?
Ans: Lambda expressions in Java are a way to represent anonymous functions — that is,
functions without a name. They provide a concise syntax for writing inline functions, making
code more readable and expressive. Lambda expressions are typically used to implement
functional interfaces, which are interfaces with a single abstract method (SAM). They essentially
serve as a replacement for anonymous classes when the only purpose of the anonymous class is
to implement a single method interface.
Lambda expressions differ from traditional anonymous classes in several ways:
1. Syntax: Lambda expressions have a much more concise syntax compared to anonymous
classes, making code easier to read and write.
Example:
// Anonymous class
Runnable r = new Runnable() {
public void run() {
System.out.println("Hello");
}
};
// Lambda expression
Runnable r = () -> System.out.println("Hello");
2. Type Inference: In lambda expressions, the compiler can often infer the type of
parameters, making it unnecessary to explicitly specify them.
3. Enclosing Scope: Lambda expressions can access variables from the enclosing scope (i.e.,
effectively final or final variables), while anonymous classes can only access final or
effectively final variables.
4. Instantiation Overhead:** Lambda expressions can be more efficient than anonymous
classes because they don't require the creation of a new class file for each instance. Instead,
they are implemented using invokedynamic bytecode instruction.
Overall, lambda expressions in Java provide a more concise and expressive way to represent
simple behaviors, especially when working with functional interfaces and collections.
Q.13.What is the purpose of meta-annotations in Java? Provide examples of meta-annotations
and explain their significance.
Ans: Meta-annotations in Java are annotations that are used to annotate other annotations. They
provide metadata about annotations themselves. The purpose of meta-annotations is to specify
how an annotation should be used or treated by the Java compiler, runtime, or tools.
Here are some common meta-annotations in Java along with their significance and examples:
1. @Retention: This meta-annotation specifies how long an annotation should be retained.
Annotations can be retained at compile-time (`RetentionPolicy.SOURCE`), class-file level
(`RetentionPolicy.CLASS`), or runtime (`RetentionPolicy.RUNTIME`). Annotations with
`RetentionPolicy.RUNTIME` can be accessed at runtime using reflection.
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
// Annotation elements here
}
2. @Target: This meta-annotation specifies the types of elements that an annotation can be
applied to, such as classes, methods, fields, etc. It restricts where the annotation can be used.
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface MyAnnotation {
// Annotation elements here
}
3. @Documented: This meta-annotation indicates that annotations with this meta-
annotation should be included in the generated JavaDoc documentation.
import java.lang.annotation.Documented;
@Documented
public @interface MyAnnotation {
// Annotation elements here
}
4. @Inherited: This meta-annotation indicates that the annotation is automatically inherited
by subclasses. It applies only to class-level annotations.
import java.lang.annotation.Inherited;
@Inherited
public @interface MyAnnotation {
// Annotation elements here
}
5. @Repeatable: This meta-annotation indicates that an annotation can be applied multiple
times to the same declaration.
import java.lang.annotation.Repeatable;
@Repeatable(MyContainer.class)
public @interface MyAnnotation {
// Annotation elements here
}
public @interface MyContainer {
MyAnnotation[] value();
}
These meta-annotations provide additional information about how annotations should be
processed or used by compilers, runtime environments, and other tools. They allow
developers to specify rules and behaviors for custom annotations, enhancing the flexibility
and usability of annotations in Java.
Q.14. Describe the significance of method references in stream operations in Java. Provide
examples of how method references can be used with streams to improve readability.
Ans: Method references play a crucial role in stream operations in Java by providing a concise
and readable way to refer to methods when performing transformations or operations on stream
elements. They allow you to pass methods as arguments to stream operations, enhancing code
clarity and reducing verbosity. Here's how method references improve readability in stream
operations:
1. Improved Clarity: Method references often make stream pipelines more readable by
expressing the intent of the operation directly. They provide a clear and concise way to
reference methods without having to define lambda expressions explicitly.
2. Elimination of Boilerplate Code: Method references help reduce boilerplate code by
avoiding unnecessary lambda expressions, especially when the lambda body simply calls a
single method.
3. Enhanced Expressiveness: Method references allow developers to focus on what needs to
be done rather than how it's done, leading to more expressive and understandable code.
Here are some examples demonstrating the use of method references in stream operations:
import java.util.Arrays;
import java.util.List;
public class MethodReferenceExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "Dave");
// Using lambda expression
names.stream()
.map(name ->name.toUpperCase())
.forEach(System.out::println);
// Using method reference
names.stream()
.map(String::toUpperCase)
.forEach(System.out::println);
// Example with instance method reference
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
.forEach(System.out::println); // instance method reference to System.out.println
}
}
In the above examples:
- `String::toUpperCase` is an example of a static method reference, where the `toUpperCase`
method of the `String` class is directly referenced.
- `System.out::println` is an example of an instance method reference, where the `println`
method of the `PrintStream` class (`System.out`) is referenced.
- Using method references instead of lambda expressions makes the code more concise and
easier to understand.
Q.15.Explain how method references can be used with the for Each method to improve code
readability. Provide examples illustrating the use of method references with for Each.
Ans: Method references in Java allow you to refer to a method without invoking it directly.
They can make code more concise and improve readability, especially when used with functional
interfaces like `Consumer` that have a single abstract method such as `accept()`.
Here is an example illustrating the use of method references with `forEach`:
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
// Using lambda expression
names.forEach(name ->System.out.println(name));
// Using method reference
names.forEach(System.out::println);
In the above code snippet, `System.out::println` is a method reference that refers to the
`println` method of the `out` object in the `System` class. This achieves the same result as
using a lambda expression but with more concise syntax, improving code readability.
Q16. How is local variable type inference denoted in Java? When should I use local variable
type inference in Java? Can var be used for method parameters or return types in Java?

Ans: Local variable type inference, introduced in Java 10 as part of Project Amber, allows you to
declare local variables without explicitly stating the type.

Local variable type inference is denoted by using the var keyword when declaring a variable. For
example, var number = 42; infers the type of number as int.

Local variable type inference can be used when the type of a variable is already clear from the
initialization expression, making explicit type declarations redundant and less readable. It is
commonly used for local variables within method bodies.

No, var can only be used for local variables declared within a method. It cannot be used for
method parameters, return types, fields, or class-level variables.
Q 17. What is Encapsulation in Java? Why is it called Data hiding?
Ans: The process of binding data and corresponding methods (behavior) together into a single
unit is called encapsulation in Java.
In other words, encapsulation is a programming technique that binds the class members
(variables and methods) together and prevents them from being accessed by other classes,
thereby we can keep variables and methods safes from outside interference and misuse.
If a field is declared private in the class then it cannot be accessed by anyone outside the class
and hides the fields within the class. Therefore, Encapsulation is also called data hiding.
Q.18.What is a Tightly encapsulated class in Java?
Ans: If each variable is declared as private in the class, it is called tightly encapsulated class in
Java. For tightly encapsulated class, we are not required to check whether class contains getter
and setter method or not and whether these methods are declared as public or not.

Q19 .What is the difference between normal variable and final variable?
Ans: The only difference between a normal variable and a final variable is that we can re-assign
the value to a normal variable but we cannot re-assign the value of a final variable once assigned
it.
Q 20.Can we call parent class constructor in sub class constructor i.e constructor chaining by
using super keyword?
Ans. Yes, we can call parent class constructor in sub class constructor by using super keyword
but super keyword must be the first statement in sub class constructor.

You might also like