top of page
Anchor 1
Frequently Asked C Interview Question & Answers

Can you override a private or static method in Java?

You cannot override a private or static method in Java. If you create a similar method with the same return type and same method arguments in child class then it will hide the superclass method; this is known as method hiding. Similarly, you cannot override a private method in subclass because it’s not accessible there. What you can do is create another private method with the same name in the child class.

Let’s take a look at the example below to understand it better.

class Base

{

private static void display()

{

System.out.println("Static or class method from Base");

}

public void print()

{

System.out.println("Non-static or instance method from Base");

}

}

class Derived extends Base

{

private static void display()

{

System.out.println("Static or class method from Derived");

}

public void print()

{

System.out.println("Non-static or instance method from Derived");

}

}

public class test

{

public static void main(String args[])

{

Base obj= new Derived();

obj1.display();

obj1.print();

}

}

Close

What is method overloading and method overriding?

Method Overloading:

  • In Method Overloading, Methods of the same class shares the same name but each method must have a different number of parameters or parameters having different types and order.

  • Method Overloading is to “add” or “extend” more to the method’s behaviour.

  • It is a compile-time polymorphism.

  • The methods must have a different signature.

  • It may or may not need inheritance in Method Overloading.

Let’s take a look at the example below to understand it better.

class Adder

{

Static int add(int a, int b)

{

return a+b;

}

Static double add( double a, double b)

{

return a+b;

}

public static void main(String args[])

{

System.out.println(Adder.add(11,11));

System.out.println(Adder.add(12.3,12.6));

}

}

Method Overriding:  

  • In Method Overriding, the subclass has the same method with the same name and exactly the same number and type of parameters and same return type as a superclass.

  • Method Overriding is to “Change” existing behaviour of the method.

  • It is a run time polymorphism.

  • The methods must have the same signature.

  • It always requires inheritance in Method Overriding.

Let’s take a look at the example below to understand it better.

class Car

{

void run()

{

System.out.println("car is running");

}

}

Class Audi extends Car

{

void run()

{

System.out.prinltn("Audi is running safely with 100km");

}

public static void main( String args[])

{

Car b=new Audi();

b.run();

}

}

Close

What are the different types of inheritance in Java?

Java supports four types of inheritance which are:

  1. Single Inheritance: In single inheritance, one class inherits the properties of another i.e there will be only one parent as well as one child class.

  2. Multilevel Inheritance: When a class is derived from a class which is also derived from another class, i.e. a class having more than one parent class but at different levels, such type of inheritance is called Multilevel Inheritance.

  3. Hierarchical Inheritance: When a class has more than one child classes (subclasses) or in other words, more than one child classes have the same parent class, then such kind of inheritance is known as hierarchical.

  4. Hybrid Inheritance: Hybrid inheritance is a combination of two or more types of inheritance.

Close

What is inheritance in Java?

Inheritance in Java is the concept where the properties of one class can be inherited by the other. It helps to reuse the code and establish a relationship between different classes. Inheritance is performed between two types of classes:

  1. Parent class (Super or Base class)

  2. Child class (Subclass or Derived class)

 

A class which inherits the properties is known as Child Class whereas a class whose properties are inherited is known as Parent class.

Close

What is the difference between abstract classes and interfaces?

Abstract Class:

  • An abstract class can provide complete, default code and/or just the details that have to be overridden.

  • In the case of an abstract class, a class may extend only one abstract class.

  • An abstract class can have non-abstract methods.

  • An abstract class can have instance variables.

  • An abstract class can have any visibility: public, private, protected.

  • If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

  • An abstract class can contain constructors.

  • Abstract classes are fast.

Interfaces:

  • An interface cannot provide any code at all, just the signature.

  • A Class may implement several interfaces.

  • All methods of an Interface are abstract.

  • An Interface cannot have instance variables.

  • An Interface visibility must be public (or) none.

  • If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

  • An Interface cannot contain constructors.

  • Interfaces are slow as it requires extra indirection to find the corresponding method in the actual class.

Close

What do you mean by an interface in Java?

An interface in Java is a blueprint of a class or you can say it is a collection of abstract methods and static constants. In an interface, each method is public and abstract but it does not contain any constructor. Thus, interface basically is a group of related methods with empty bodies.

Example:

public interface Animal

{

public void eat();

public void sleep();

public void run();

}

Close

What is abstraction in Java?

Abstraction refers to the quality of dealing with ideas rather than events. It basically deals with hiding the details and showing the essential things to the user. Thus you can say that abstraction in Java is the process of hiding the implementation details from the user and revealing only the functionality to them. Abstraction can be achieved in two ways:   

  1. Abstract Classes (0-100% of abstraction can be achieved)

  2. Interfaces (100% of abstraction can be achieved)

Close

What are the key features of Python?

  • Python is an interpreted language. That means that, unlike languages like C and its variants, Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby.

  • Python is dynamically typed, this means that you don’t need to state the types of variables when you declare them or anything like that. You can do things like x=111 and then x="I'm a string" without error.

  • Python is well suited to object orientated programming in that it allows the definition of classes along with composition and inheritance. Python does not have access specifiers (like C++’s public, private).

  • In Python, functions are first-class objects. This means that they can be assigned to variables, returned from other functions and passed into functions. Classes are also first class objects.

  • Writing Python code is quick but running it is often slower than compiled languages. Fortunately,Python allows the inclusion of C based extensions so bottlenecks can be optimized away and often are. The numpy package is a good example of this, it’s really quite quick because a lot of the number crunching it does isn’t actually done by Python.

  • Python finds use in many spheres – web applications, automation, scientific modeling, big data applications and many more. It’s also often used as “glue” code to get other languages and components to play nice.

Close

How is Python executed?

Python files are compiled to bytecode. Which is then executed by the host.

Alternate Answer:

Type python .pv at the command line.

Close

How Python is interpreted?

Python language is an interpreted language. Python program runs directly from the source code. It converts the source code that is written by the programmer into an intermediate language, which is again translated into machine language that has to be executed.

Close
bottom of page