Accenture First & Second Round: Technical Interview Questions
Java Interview questions:
1) Why is Java called platform independent?
Java is platform independent that means we can execute our code in any operating system either it is mac, Window or Linux. Java is Platform independent because when we write code, then its compiler converts it into bytecode and this bytecode can be executed on any platform (JDK should be installed in that OS).
2) What do you understand by Exception Handling?
Exception handling is a process of handling exceptions occurs during the execution of a program. Due to the occurrence of exception, execution of programs get halted, so it is very important to handle these exceptions so that program can be executed smoothly. We can handle the exceptions by using five keywords: try, catch, throw, throws, and finally.
3) What is checked and unchecked exception?
- Checked exception: If the exception occurs or checked at compile time during the execution of a program, it is called as the checked exception. We should handle these exceptions using try-catch block or using throws keyword.
E.g., if someone tries to read a file which is not present then it will throw a checked exception at compile time FileNotFoundException - Unchecked exceptions: If the exception is not checked at compile time and occurred at runtime then this type of exception is called an unchecked exception. This type of exceptions occur due to an error in the logic of the code. If we do not handle this type of exception then also compiler will not give a compilation error.
E.g. ArithmeticException
4) What are the reasons behind the occurrence of an exception?
Following are the reasons behind the occurrence of an exception:
- Accessing a file, which does not exist
- Dividing a variable by zero
- Inserting an element in the array outside the range
- If throw statement occurs
- Abnormal execution condition captured by JVM
5) What is OOP concept?
OOP stands for Object-Oriented Programming. Object-Oriented Programming is a coding practice which works with objects and class. Java is one of the programming languages which is based on these concepts. The basic OOP concepts are:
- Object
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
6) Explain the basic features of OOPs.
Following are the basic features of OOPs:
- Object: – An object is a physical entity which has a state and behaviour. It occupies space in memory. It is a sample of a class. Object helps to access the methods and variables in the program.
- Class – A Class is “collection of objects.” A class is a logical entity, which does not take any space. A class includes all the data and methods which shows the behaviour of an object.
- Inheritance – Inheritance is a process by which one class can have all properties of other class. Inheritance increases the code reusability. There are two terms used
- Child class (Subclass): Class which inherits other class, called as Child class or derived class.
- Parent class (Superclass): A class which got inherited by another class is termed as parent class or Base class.
- Polymorphism: – Polymorphism is a method of performing “a single task in different ways.” Polymorphism enables a programmer to use methods or operators in different ways. In Java, we use method overloading and overriding to obtain the polymorphism.
- Abstraction: If we show only functionality and hide the explanations or details then this process is called as Abstraction. For achieving the abstraction, we use two ways in Java
- Abstract class
- Interface
- Encapsulation: Encapsulation is a process of enclosing the data and code together to form a single unit. It makes the data safer within the code for any modification. For achieving the encapsulation, we can declare the data variables of class as private.
7) Differentiate between class and object.
The class and object both are the features of OOPs concepts. The basic differences between both features are given below:
- The Class is a logical entity whereas Object is a physical quantity.
- Class does not occupy memory at the time of creation whereas Object occupied space in memory when it is created.
- For declaring a class, we use a ‘class’ keyword followed by a class name, whereas we can create the object using the ‘new’ keyword in Java.
- A Class is like a factory which generates object and object are the instances of the class.
8) What is encapsulation in Java?
Encapsulation is a process of enclosing the data and code together to form a single unit. The best example to understand the encapsulation is a capsule which contains the medicine in it.
- If we declare all the data members of the class as private, then it is called a fully encapsulated class in Java, and then we can use getter and setter method to access it.
- One of the examples of the fully encapsulated class is Java Bean class.
- Encapsulation keeps its data hide from other class hence it is also called as data-hiding.
Example for encapsulation:
- class EncapsulationEg{
- private String empname;
- private int empage;
- private int empid;
- public String getEmpName() //getter method
- {
- return empname;
- }
- public int getEmpAge()
- {
- return empage;
- }
- public int getEmpId()
- {
- return empid;
- }
- public void setEmpName(String setvalue) //setter methods
- {
- empname=setvalue;
- }
- public void setEmpAge(int setvalue){
- empage=setvalue;
- }
- public void setEmpId(int setvalue){
- empid=setvalue;
- }
- }
- public class TestEncapsulation{
- public static void main(String args[]){
- EncapsulationEg en= new EncapsulationEg();
- en.setEmpName(“Alvin”);
- en.setEmpAge(22);
- en.setEmpId(12568);
- System.out.println(“Employee Name: “ +en.getEmpAge());
- System.out.println(“Employee Age: “ +en.getEmpAge());
- System.out.println(“Employee ID: “ +en.getEmpId());
- }
- }
Output:
Employee Name: 22 Employee Age: 22 Employee ID: 12568
9) What is Recursion and recursive function in Java?
Recursion is a process of calling a method by itself continuously till not get termination point. A method which calls itself is called as a recursive method.
Syntax:
- Return-type method_name()
- {
- // Code to be executed
- method_name(); // same name of calling method }
10) How can you differentiate between C, C++, and Java?
There are the following differences between the C, C++, and Java language.
Index | C language | C++ | Java |
---|---|---|---|
1. | C language is a procedural language. | C++ is an object-oriented language. | Java is also an object-oriented language (not pure as it also supports primitive data types). |
1. | C language is platform dependent. | C++ is platform dependent. | Java is platform independent language. |
1. | C language supports pointers. | C++ language also supports pointers. | Java does not support pointers. |
1. | We cannot create our own package in C language | In C++ language also, we cannot create our package. | In the Java language, we can create our package and can specify the classes. |
1. | In C, there is no any concept of inheritance. | In C++, we can use multiple inheritance. | Java does not support multiple inheritance. |
11) What do you understand by runtime polymorphism?
Polymorphism is a method of performing “a single task in different ways.” Polymorphism is of two types
- Runtime Polymorphism
- Compile-time polymorphism
Here we will discuss runtime polymorphism.
Runtime Polymorphism– We can achieve runtime Polymorphism by method overriding in Java. And method overriding is a process of overriding a method in the subclass which is having the same signature as that of in superclass.
- class A{ //Superclass
- void name()
- {
- System.out.println(“this is student of Superclass”);
- }
- }
- class Student extends A //Subclass
- {
- void name(){ // method Override with same signature(runtime polymorphism)
- System.out.println(“this is student of subclass”);
- }
- public static void main (String[] args) {
- A a= new A(); // refrence of A class
- A b= new Student(); // refrence of student class
- a.name();
- b.name();
- }
- }
Output:
this is student of Superclass this is student of subclass
12) How can you differentiate between method overloading and method overriding?
No. | Method overloading | Method overriding |
---|---|---|
1. | The process of calling two methods having the same name with different parameters is called method overloading (in the same class) | The process of calling two methods, one in the subclass and other in the superclass, having the same signature is called as method overriding. |
2. | It can be accessed within a class. | Method overriding requires two classes to be accessed which having IS-A relationship. |
3. | Return type may be changed or may remain same with different parameters | Return type should be the same for both methods. |
4. | Method overloading is a concept of compile-time polymorphism. | Method overriding is a concept of method overriding. |
5. | e.g. class A{ void m1() {// codes…….} Void m1 (int a) {//code………} |
e.g. class A { void m1(){ // code…………} } Class B extends A{ Void m1(){ // code………..} |
13) What are the keyword “super” and “this” in Java?
super keyword: “super” is a keyword in Java which is used to give reference to the object of parent class. “super” keyword cannot be used as an identifier as it is reserved keyword in Java.
this Keyword: “this” keyword in Java is used to refer to the object of the current class. The ‘this’ keyword cannot be used as an identifier as it is a reserved keyword in Java.
14) What is an interface in Java? Can we implement multiple interfaces in one class?
Interface in Java is a way to achieve the abstraction. The Interface is like a class but not exactly as Interface also can have methods and variable as the class does but Interface only contain method signature does not have the body.
- The Interface cannot be instantiated in Java.
- The Interface contains methods which are public and abstract (by default).
- A class can implement an interface.
- For declaring an interface, we use the keyword interface.
Syntax:
- interface Interface_Name{
- //Methods
- }
We can implement multiple interfaces in one class and parent interfaces can be declared using a comma(,) operator.
Syntax:
- public class A implements C, D {
- Code
- }
15) Explain inheritance in Java? How can it be achieved?
- Inheritance in Java is a process by which one class can have all properties of other class. That means one class inherits all the behaviour of the other class.
- Inheritance increases the code reusability.
- Inheritance is an important feature of OOP concept.
- Inheritance is also a representation of the IS-A relationship
There are two terms used in inheritance:
- Child class (Subclass): Class which inherits other class, called a Child class or derived class.
- Parent class (Superclass): A class which got inherited by another class is termed as parent class or Base class.
The Syntax of java inheritance:
- Class A extends B // Here A represents subclass and B represent Superclass
- {
- // Code
- }
16) Can we use multiple inheritance in Java? Explain with reason?
No, we cannot use multiple inheritance in java as it creates ambiguity and diamond problem in the program. To overcome this problem, we can use interface in Java.
Let suppose class A inherits the two parent class B and C in which a method with the same name is present in both the classes and hence when we try to override that method it will create confusion for the compiler and will give the compilation error. Therefore, Java doesn?t support multiple inheritance.
17) What can we do if we want to access a private member of a class?
We can access private members of the class by using public getters and setters from outside the class in Java.
18) What is the significance of “static” keyword?
- Static keyword in Java is a non-access modifier which can be used with the block, variable, methods, and nested classes.
- Static Keywords are the part of the class, and it does not belong to the instance of the class.
- We use static keyword in java with variables, block, and method for achieving memory management.
- Java static property can be shared by all the objects.
- For accessing the static members, we don’t need to create the instance of the class.
19) What is “Collection Framework” in Java?
Collection Framework in Java is an architecture for storing the classes, and interfaces and manipulating the data in the form of objects. There are two main interfaces in Collection Framework that are:
- Java.util.Collection
- Java.util.Map
20) What is List interface in collections?
List interface is an interface in Java Collection Framework. List interface extends the Collection interface.
- It is an ordered collection of objects.
- It contains duplicate elements.
- It also allows random access of elements.
Syntax:
- public interface List<E> extends Collection<E>
21) What do you understand by object cloning?
Object cloning is a mechanism of creating the same copy of an object. For object cloning, we can use clone() method of the Object class. The class must implement the java.lang.Clonable interface, whose clone we want to create otherwise it will throw an exception.
Syntax of clone() method:
- protected Object clone() throws CloneNotSupportedException
22) Can we insert duplicate values in Set?
We cannot insert duplicate elements in Set. If we add a duplicate element, then the output will only show the unique elements.
23) What is the difference between Collections, and Collection in Java?
Collection and collections both are the part of Java Collection Framework, but the primary differences between both are given below:
- A Collection is an interface in java and Collections is a class of collection framework.
- The Collection interface provides the methods that can be used for data structure whereas Collections class provides the static methods which can be used for various operation on a collection.
24) What is “Diamond problem” in Java? How can it be removed?
The Diamond problem occurs in multiple inheritance, but Java does not allow multiple inheritance. In case of Java, it can occur with interfaces. When we implement two interfaces which are having methods with the same signature then it creates ambiguity for the compiler, and it gives compile time error. Its structure looks like diamond so it is called as Diamond problem.
E.g. Let’s take an example which will show the diamond problem.
- interface InterfaceA {
- default public void m1() { System.out.println(“This is interface A!”); }
- }
- interface InterfaceB {
- default public void m1(){ System.out.println(“This is interface B!”); } //same signature as interface InterfaceA?
- }
- public class Simple implements InterfaceA, InterfaceB {
- public static void main(String args[]) {
- Simple s1= new Simple();
- s1.m1(); // It will give error..
- }}
Error: Simple.java:10: error: class Simple inherits unrelated defaults for m1() from types InterfaceA and InterfaceB
25) What is an abstract class in Java?
- An Abstract class is used to achieve abstraction in Java. If we use the keyword “abstract” with the class name, then it is called as an abstract class.
- An Abstract class can have only methods without body or can have methods with some implementation.
- The Abstract class cannot be instantiated
- It’s not necessary that an abstract class should have an abstract method.
Syntax:
- abstract class Student{
- }
26) What is deadlock condition in multithreading?
A Deadlock condition occurs in the case of multithreading. It is a condition when one thread is waiting for an object lock, which is already acquired by another thread and the second thread is waiting for lock object which is taken by the first thread, so this is called deadlock condition in Java.
27) Differentiate between Collection and array.
The Collection and Array both are responsible for storing the data, but the fundamental differences between both are given below:
- Arrays are always of fixed size, we cannot change its size at runtime, but In Collection, size can be changed dynamically.
- Arrays can only store homogeneous or similar type objects, but in Collection, both homogeneous and heterogeneous objects can be stored.
- Arrays cannot provide the “ready-made” methods for user requirements as sorting, searching, etc. but Collection includes readymade methods to use.
- Arrays are good in performance as compare to Collection but Array take more space in memory in comparison to Collection.
C/C++ Interview Questions
28) What is call by value and call by reference?
There are two ways in C language to pass the values in the function that are:
- Call by Value
- Call by reference
Call by value: In call by value, a copy of the value is passed to the function so, if we change in formal parameters it will not affect the actual parameters. When we change in the value of the formal parameter in the “calling function,” it just takes it as the local variable and does not affect the actual value.
Call by reference: In call by reference, an address of that value is passed to the function so, if we make any changes in formal parameters it will affect the actual parameter as well.
29) Explain dynamic memory and static memory allocation.
There are two types of allocation of memory:
- Static memory allocation: The allocation of memory at the starting of the program is called static memory allocation. In static memory allocation size remains fix i.e. we cannot change the size at runtime. Static memory allocation is used in Array.
- Dynamic memory allocation: The allocation of memory at runtime is called as dynamic memory allocation. We can specify a size at runtime as per need. The memory gets allocated from heap area, and it can also be deallocated from same. Dynamic memory allocation used in pointers.
30) What is difference between “var++” and “++var”?
The expressions, ( var++)and ( ++var) both are used for the incrementing the value of variable var.
The main difference between both is that, (var++ ) it gives the evaluation of statement first and then it increments the value by one whereas (++var) is used as it increments the value by one before the evaluation of the expression.
E.g.
- #include <stdio.h>
- int main()
- {
- int a,b;
- a=5, b=1;
- printf(“%d %d”, a++, a); //will generate 5, 6 as output
- printf(“%d %d”, ++b, b); //will generate 2, 2 as output
- )
31) Differentiate between class and structure in C++.
The class and structure both are approximately equivalent to each other. But the main differences between both the terms are given below.
- The members of the class are accessed as private by default whereas members of the structure are accessed public by default.
- Class in C++ is a group of objects, related variables, and functions, whereas Structure is a user-defined data type which performs its operation
- We can declare a class using the keyword ‘class’, whereas we can declare a structure using the keyword ‘struct.’
- The objects of the class, created at heap area of the memory, whereas the object of the structure created on stack area of memory.
32) What are the access specifiers in C++?
We use access specifier to prevent the access of the class members or to implement the feature of Data Hiding. In C++, there are some keywords used to access specifier within class body, which are given below:
- Public: If we specify a class member as public then it will be accessible from anywhere within a program. We can directly access the private member of one class from another class.
- Private: If we specify any class member as private then it will only be accessed by the function of that class only. We cannot access the private member of the class outside the class.
- Protected: Protected access specifier is mainly used in case of inheritance. If we define the class member as protected, then we cannot access class member outside the class, but we can access it by subclass or derived class of that class.
E.g. Below is the example to show the access specifier in C++.
33) Why we use of “getch()” in a program?
The getch() function is a predefined library function which is used to take a input character from the screen, and it holds the screen till it not get character from input.
- #include<stdio.h>
- main()
- {
- printf(“enter the character. \n”);
- getch();
- }
Output:
enter the character.
34) What does “main()” function in C++? Can we run a program without main method?
In C++ programming language the main() function is the entry point of that program. When we start execution of any program, execution directly goes to the main() in the program.
Syntax for main():
- void main(){
- ///Program body;
- }
35) What is the declaration and definition of a variable?
Declaration of Variable: Declaring a variable or function is that we are declaring the name and its type so that it can be used in the program. The Compiler can understand and use those variables and functions. Declaring a member gives its representation in that program.
e.g.
- extern int x;
- extern char y; // It tells the compiler that there are two variables x and y of types char and int
Definition of Variable: Defining a variable means, we are providing some value to it, or we are initializing it with some value. Defining a variable or any member of the program gives the complete information about it.
e.g.
- int x= 2;
- char y = ‘A’, ‘B’; // It defines the variable x and y by giving its value or body.
36) What do you understand by Friend function?
A friend() function in C++ is a function which can access private and protected members of another class in which it is declared as a friend. We can declare a function as a friend function by using keyword friend. A friend function can be of two types:
- Method of another class
- A global function
Syntax:
- class Class_Name {
- //Line of code
- friend return-type func_name() ;}
The above-declared friend function can use private members of the class.
37) What is memory allocation in C?
Memory allocation is a process of allocating or reserving some portion of the memory (or as per need) for the execution of the code.
There are mainly two types of memory allocation in C:
- Static memory allocation
- Dynamic memory allocation
- Static memory allocation: The allocation of memory at the starting of the program is called static memory allocation. In static memory allocation size remains fix, i.e., we cannot change the size at runtime. Static memory allocation is used in Array.
- Dynamic memory allocation: The allocation of memory at runtime is called as dynamic memory allocation. We can specify a size at runtime as per need. The memory gets allocated from heap area, and it can also be deallocated from same. Dynamic memory allocation used in pointers. There are four types of the predefined function used for dynamic memory allocation
- malloc()
- calloc()
- free()
- realloc()
38) Differentiate between malloc() and calloc()?
The malloc() and calloc() are the two pre-defined library function available in <stdlib.h> Library. Both the functions are used for dynamic memory allocation for the execution of the program. The basic differences between the malloc() and calloc() are given below:
- The malloc() stands for memory allocation while calloc() stands for contiguous allocation
- The malloc() allocates a single block of memory whereas calloc() allocates multiple blocks of memory.
- The malloc() takes only one argument, i.e., size of given block. Whereas calloc() takes two arguments, i.e., a number of blocks to be allocated and size of all blocks.
- The malloc() function is faster than calloc() function for memory allocation.
Syntax for malloc() function:
- ptr= (type_cast*) malloc( size_t size );
Syntax for calloc() function:
- ptr= (type_cast *) calloc(n , size_t size );
39) Why is C a procedural language?
Procedural language defines a language which follows some pattern, hence C language is called as procedural language as in C language a programmer must follow a specific pattern or flow of the program. C language follows a top-down approach to solve a problem. It mainly focuses on the flow of the program rather than the data.