SOFTEON INTERVIEW QUESTIONS FOR FRESHERS WITH ANSWERS
This blog explains about SOFTEON INTERVIEW QUESTIONS FOR FRESHERS WITH ANSWERS and is given below :
_______________________________________________________________________________
1. Find the duplicate elements in a string in java program .
// Java program to find the first
// repeated character in a string import java.util.*; class Main { // This function prints the first repeated // character in str[] static char firstRepeating(char str[]) { // Creates an empty hashset HashSet<Character> h = new HashSet<>(); // Traverse the input array from left to right for (int i=0; i<=str.length-1; i++) { char c = str[i]; // If element is already in hash set, update x // and then break if (h.contains(c)) return c; else // Else add element to hash set h.add(c); } return ‘\0’; } // Driver method to test above method public static void main (String[] args) { String str = “geeksforgeeks”; char[] arr = str.toCharArray(); System.out.println(firstRepeating(arr)); } } |
Output:
e
_______________________________________________________________________________
2. Explain briefly about Fibonacci series in Java program .
Fibonacci series in Java
In fibonacci series, next number is the sum of previous two numbers
For example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The first two numbers of fibonacci series are 0 and 1.
There are two ways to write the fibonacci series program in java:
- Fibonacci Series without using recursion
- Fibonacci Series using recursion
Fibonacci Series in Java without using recursion
The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, …
Let’s see the fibonacci series program in java without using recursion.
- classFibonacciExample1{
- publicstatic void main(String args[])
- {
- int n1=0,n2=1,n3,i,count=10;
- out.print(n1+” “+n2);//printing 0 and 1
- for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
- {
- n3=n1+n2;
- out.print(” “+n3);
- n1=n2;
- n2=n3;
- }
- }}
Output:
0 1 1 2 3 5 8 13 21 34
Fibonacci Series using recursion in java
Let’s see the fibonacci series program in java using recursion.
- classFibonacciExample2{
- static int n1=0,n2=1,n3=0;
- static void printFibonacci(int count){
- if(count>0){
- n3 = n1 + n2;
- n1 = n2;
- n2 = n3;
- out.print(” “+n3);
- printFibonacci(count-1);
- }
- }
- publicstatic void main(String args[]){
- intcount=10;
- out.print(n1+” “+n2);//printing 0 and 1
- printFibonacci(count-2);//n-2 because 2 numbers are already printed
- }
- }
Output:
0 1 1 2 3 5 8 13 21 34
_______________________________________________________________________________
3 . Explain briefly about OOPS concept .
Object Oriented Approach :
Java is an object oriented language because it provides the features to implement an object oriented model. These features includes Abstraction, encapsulation, inheritance and polymorphism.
OOPS is about developing an application around its data, i.e. objects which provides the access to their properties and the possible operations in their own way.
ABSTRACTION
One of the most fundamental concept of OOPs is Abstraction. Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user.
Abstraction is selecting data from a larger pool to show only the relevant details to the object.
It helps to reduce programming complexity and effort.
In Java, abstraction is accomplished using Abstract classes and interfaces.
It is one of the most important concepts of OOPs.
ENCAPSULATION :
Encapsulation is:
- Binding the data with the code that manipulates it.
- It keeps the data and the code safe from external interference
Encapsulated code should have following characteristics:
- Everyone knows how to access it.
- Can be easily used regardless of implementation details.
- There shouldn’t any side effects of the code, to the rest of the application.
The idea of encapsulation is to keep classes separated and prevent them from having tightly coupled with each other.
INHERITANCE :
Inheritance is a mechanism in which one class acquires the property of another class. For example, a child inherits the traits of his/her parents. With inheritance, we can reuse the fields and methods of the existing class. Hence, inheritance facilitates Reusability and is an important concept of OOPs
- Inheritance is the mechanism by which an object acquires the some/all properties of another object.
- It supports the concept of hierarchical classification.
POLYMORPHISM
Polymorphism is a OOPs concept where one name can have many forms.
For example, you have a smartphone for communication. The communication mode you choose could be anything. It can be a call, a text message, a picture message, mail, etc. So, the goal is common that is communication, but their approach is different. This is called Polymorphism.
Polymorphism means to process objects differently based on their data type.
- In other words it means, one method with multiple implementation, for a certain class of action. And which implementation to be used is decided at runtime depending upon the situation (i.e., data type of the object)
This can be implemented by designing a generic interface, which provides generic methods for a certain class of action and there can be multiple classes, which provides the implementation of these generic methods
____________________________________________________________________
4. What do you mean by Implements keyword in Java .
The implements keyword is used to make a class adheres to contract defined by an interface. The implemented class must provide concrete implementation for the methods defined by the interface. If not, the class must be abstract.
A Java class can implement multiple Java Interfaces. It is necessary that the class must implement all the methods declared in the interfaces. The interface allows sending a message to an object without concerning which classes it belongs. Class needs to provide functionality for the methods declared in the interface.
A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.
Example
/* File name : MammalInt.java */
public class MammalInt implements Animal {
public void eat() {
System.out.println(“Mammal eats”);
}
public void travel() {
System.out.println(“Mammal travels”);
}
public int noOfLegs() {
return 0;
}
public static void main(String args[]) {
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
This will produce the following result −
Output
Mammal eats
Mammal travels
_______________________________________________________________________________
5. Write the difference between Structure and Java Class .
Difference between Classes and Structures.
Class can create a subclass that will inherit parent’s properties and methods, whereas Structure does not support the inheritance.
A class has all members private by default.
Abstract is a class where members are public by default.
The major difference like class provides the flexibility of combining data and methods (functions ) and it provides the re-usability called inheritance.
Struct should typically be used for grouping data.
The technical difference comes down to subtle issues about default visibility of members.
Here you can see some of the Difference between Class and Structure.
Class is pass-by-reference and Struct is pass-by-copy, it means that, Class is a reference type and its object is created on the heap memory where as structure is a value type and its object is created on the stack memory. More about
Difference between a Value Type and a Reference Type
Class can create a subclass that will inherit parent’s properties and methods, whereas Structure does not support the inheritance.
A class has all members private by default. A struct is a class where members are public by default.
_______________________________________________________________________________________
REFERENCES :
https://www.geeksforgeeks.org/find-the-first-repeated-character-in-a-string/
https://www.javatpoint.com/fibonacci-series-in-java
https://www.guru99.com/java-inheritance-polymorphism.html
https://www.guru99.com/java-data-abstraction.html
https://www.tutorialspoint.com/java/java_interfaces.htm
http://net-informations.com/faq/oops/struct.htm
_______________________________________________________________________________________