Coding Interview Questions for Freshers _ PrepInsta
Coding Interview Questions for Freshers _ PrepInsta
As a newcomer, answering technical questions head-on can be nerve-wracking, so to help you out, we’ve
compiled a list of the most often asked Coding Interview Questions For Freshers. Coding can be asked in
primarily four different languages. These are:-
1. C
2. C++
3. JAVA
4. Python
Some companies can allow you to choose your preferred language and then ask you questions in that language. Some people
do not agree.
But to clear a face-to-face interview you should know at least two coding languages. Don’t worry we are here to help you out
in this and to provide you commonly asked Codin Interview Questions For Freshers.
When the Header file is included within double quotes (“ ”), the compiler searches first in the working directory for
the particular header file.
But when the Header file is included within angular braces (<>), the compiler only searches in the working directory for
the particular header file.
Example – In a 32-bit processor, storage space for the int data type is 4.When we use it with modifier the storage space
change as follows:
This indicates the point that the new line starts to the compiler and the output is created accordingly.
The following sample program demonstrates the use of the newline escape sequence.
/*
* C Program to print string
*/
#include
#include
int main(){
printf("String 01 ");
printf("String 02 ");
printf("String 03 \n");
printf("String 01 \n");
printf("String 02 \n");
return 0;
}
int main() {
int num;
scanf("%d", &num);
return 0;
int addNumbers(int n) {
if (n != 0)
else
return n;
OUTPUT
OUTPUT
10 8 6
The declaration of a variable is merely specifying the data type of a variable and the variable name.
As a result of the declaration, we tell the compiler to reserve the space for a variable in the memory according to the
data type specified.
Example :
int result;
char c;
int a,b,c;
Also, note that as a result of the declaration, the value of the variable is undetermined.
Whereas, a definition is an implementation/instantiation of the declared variable where we tie up appropriate value to the
declared variable so that the linker will be able to link references to the appropriate entities.
Result = 10;
C = ‘A’;
In C++, we can define our own constants using the #define preprocessor directive.
Example:
#include
#define PI 3.142
int main ()
{
float radius =5, area;
area = PI * r * r;
cout<<”Area of a Circle = “<<area;
}
Output: Area of a Circle = 78.55
As shown in the above example, once we define a constant using #define directive, we can use it throughout the program
and substitute its value.
This way is similar to that of declaring a variable, but with a const prefix.
In the above examples, whenever the type of a constant is not specified, the C++ compiler defaults it to aninteger
type.
While passing parameters to the function using “Pass by Value”, we pass a copy of the parameters to the function.
Hence, whatever modifications are made to the parameters in the called function are not passed back to the
calling function.
int main()
int x = 1,y=3,z=4;
printFunc(x,y,z);
cout<<”x = “<<x<<”\ny = “<<y<<”\nz = “<<z;
}
Output:
x=1
y=3
z=4
As seen above, although the parameters were changed in the called function, their values were not reflected in the calling
function as they were passed by value.
However, if we want to get the changed values from the function back to the calling function, then we use the “Pass
by Reference” technique.
Output:
x=2
y=6
z=8
As shown above, the modifications done to the parameters in the called functions are passed to the calling function when we
use the “Pass by reference” technique.
This is because using this technique we do not pass a copy of the parameters but we actually pass the variable’s reference itself
Virtual functions are used with inheritance, they are called according to the type of the object pointed or referred to,
not according to the type of pointer or reference.
Following things are necessary to write a C++ program with runtime polymorphism (use of virtual functions)
1) A base class and a derived class.
2) A function with same name in base class and derived class.
3) A pointer or reference of base class type pointing or referring to an object of derived class.
For example, in the following program bp is a pointer of type Base, but a call to bp->show() calls show() function of Derived
class, because bp points to an object of Derived class.
#include
using namespace std;
class Base {
public:
virtual void show() { cout<<" In Base \n"; }
};
Output:
In Derived
Operators are specific operands in C++ that are used to perform specific operations to obtain a result.
The different types of operators available for C++ are Assignment Operator, Compound Assignment Operator,
Arithmetic Operator, Increment Operator, and so on.
For example arithmetic operators, you want to add two values a+b
#include
Using namespace std;
main ()
{
int a= 21 ;
int b= 10 ;
int c;
c= a + b;
cout << "Line 1- Value of c is : " << c << endl ;
return 0;
}
Example:
public class Addition{ //Class name declaration
int a = 5; //Variable declaration
int b= 5;
public void add(){ //Method declaration
int c = a+b;
}
}
2. What is an Object?
Answer:-
An instance of a class is called an object. The object has a state and behavior.
Whenever the JVM reads the “new()” keyword then it will create an instance of that class.
Example:
The above code creates the object for the Addition class.
Inheritance means one class can extend to another class. So that the codes can be reused from one class to another class.
The existing class is known as the Superclass whereas the derived class is known as a subclass.
Example:
Super class:
public class Manupulation(){
}
Sub class:
public class Addition extends Manipulation(){
}
Inheritance is applicable for public and protected members only. Private members can’t be inherited.
4. Write a Java Program to reverse a string without using String inbuilt function reverse().
Answer:-
There are several ways with which you can reverse your string if you are allowed to use the other string inbuilt functions.
In this method, we are initializing a string variable called str with the value of your given string.
Then, we are converting that string into a character array with the toCharArray() function. Thereafter, we are using for loop to
iterate between each character in reverse order and printing each character.
Output:
varuaS tekaS
5. Write a Java program to print the Fibonacci series up to a given number or create a
simple Java program to calculate Fibonacci number
Answer:-
import java.util.Scanner;
/**
* Java program to calculate and print Fibonacci number using both recursion
* and Iteration.
* Fibonacci number is sum of previous two Fibonacci numbers fn= fn-1+ fn-2
* first 10 Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
*
* @author Javin
*/
public class FibonacciCalculator {
/*
* Java program for Fibonacci number using recursion.
* This program uses tail recursion to calculate Fibonacci number
* for a given number
* @return Fibonacci number
*/
public static int fibonacci(int number){
if(number == 1 || number == 2){
return 1;
}
/*
* Java program to calculate Fibonacci number using loop or Iteration.
* @return Fibonacci number
*/
public static int fibonacci2(int number){
if(number == 1 || number == 2){
return 1;
}
int fibo1=1, fibo2=1, fibonacci=1;
for(int i= 3; i<= number; i++){
}
return fibonacci; //Fibonacci number
Output:
Enter number upto which Fibonacci series to print:
12
Fibonacci series upto 12 numbers :
1 1 2 3 5 8 13 21 34 55 89 144
Interview Coding Questions for Python language
1. How to overload constructors or methods in Python?
Answer:-
Whenever we try to instantiate an object __init__() is automatically invoked by python to initialize members of an
object. We can’t overload constructors or methods in Python.
class student:
def __init__(self,name):
self.name = name
self.name = name
self.email = email
#st = student(“rahul”)
st = student(“rahul”, “[email protected]”)
print(st.name)
Output:
rahul
It randomizes the items in the array. This method is present in the random module. So, we need to import it and then we can
call the function. It shuffles elements each time when the function calls and produces different output.
import random
list = [12,25,15,65,58,14,5,]; print(list) random.shuffle(list) print (“Reshuffled list : \n”, list)
[12, 25, 15, 65, 58, 14, 5]
Reshuffled list :
[58, 15, 5, 65, 12, 14, 25]
The read-only, write-only, read-write and append mode. ‘r’ is used to open a file in read-only mode, ‘w’ is used to open a file in
write-only mode, ‘rw’ is used to open in reading and write mode, ‘a’ is used to open a file in append mode. If the mode is not
specified, by default file opens in read-only mode.
Input : [1, 2, 3]
Output : [3, 2, 1]
def swapList(newList):
size = len(newList)
# Swapping
temp = newList[0]
newList[0] = newList[size - 1]
newList[size - 1] = temp
print(swapList(newList))
OUTPUT:-
[24, 35, 9, 56, 12]
5. Python program to print even length words in a string.
Answer:-
def printWords(s):
# if length is even
if len(word)%2==0:
print(word)
# Driver Code
s = "i am muskan"
printWords(s)
OUTPUT:
am
muskan