Latest Posts

Variables in Python

SDET

Variables in Python

There are three types of variable in Python
  • Static variable
  • Instance variable
  • Local variable
Instance variable
  • Instance variable belongs to instance of the class.
  • For every object creation separate copy of instance variable is created.
  • Instance variable is represented by self parameter.
Creation of Instance variable
  • Inside constructor using self parameter.
  1. class Student :
  2.     def __init__(self,name,age):
  3.         self.name=name       
  4.         self.age=age 
  • Inside Instance method using self parameter.
  1. class Student :
  2.     def display(self, address, id):
  3.         self.address=address       
  4.         self.id=id
  • Outside the class using object reference.
  1. class Student :
  2.     def __init__(self,name,age):
  3.         self.name=name       
  4.         self.age=age
  5.     def display(self, address, id):
  6.         self.address=address       
  7.         self.id=id
  8. s1=Student('ritesh', 25)
  9. s1.height=5.6     #  s.height is instance variable outside the class

Self parameter and Constructor in Python

SDET
  • Self in Python is reference variable which always point toward current object inside the class.
  • Self is used to access instance variables and instance methods of object inside the class.
  • Whenever we create constructor and methods inside the class, it accepts self as a first parameter.
  • Self is used to create instance variables.
  1. class Student :
  2.     def __init__(self,name,age):
  3.         self.name=name       
  4.         self.age=age       # self.name and self.age is instance variable.
  5.     def display():
  6.         print('Name of student is '+self.name)   #calling instance variable by self parameter
  7.         print('Name of student is '+str(self.age)) 
  8. s1 = Student('ritesh',25)
  9. s1.display()
Constructor:
  • constructor is special method in Python which is automatically called when we create object of the class.
  • constructor is represented by __init__ method in Python.
  • It is used to declare and initialize instance variables.
  • In case we don't provide constructor then Python provide default constructor.
  • It is executed once at the time of object creation.

Class and Object in Python

SDET

  • Class in blueprint, design of any object. suppose you want to build a house for yourself. For building house you need plan, design and blueprint of the house to build the house. In this case plan, design and blueprint is class and Physical built House is Object.
  • Class represent properties(Attributes) and action(behavior) of object. 
  • Properties are represented by variables and actions are represented by methods.
  • So class contains both variables and methods.
    How to create class in Python
    Class in created using 'class' keyword in Python.
    Syntax
    class className
    Example:
    class Car:
         def __init__(self):
                print('Inside constructor')
def m1(self):
print('Inside method')
  • In this examples Car is name of class and m1 is method name.
    Syntax of Object creation in Python
    reference variable = className()
  • In above example we can create object of Car class-
    Audi = Car()

Introduction to API

SDET

  API(Application Programming Interface) is Intermediate application which enables communication between two software applications. Let's understand it with MakeMyTrip application. For booking flights ticket and hotel room you might have used MakeMyTrip. Let's say you want to book room for accommodation in any hotel in particular city but problem is that how can you book room in any hotel through MakeMyTrip because it has not access to any hotel booking applications server. They are separate applications and how can any hotel application expose their server to any third party application. Here API comes into picture.

To solve this problem hotel applications expose API which enables communication between application server to third party applications to book ticket. So when you search ticket for hotel booking, MakeMyTrip send request to API of different hotel booking applications which communicates to application server to check if there is any room available. If room available then it'll return response to make my trip application to book the tickets.

API follows HTTP protocol to enables communication between client and server.

How to reverse the given number

SDET

 We are going to see how to reverse the number is Java.

Suppose We have number 24586 so reverse of this number will be 68542.
To reverse the number we are going to use following logic.
1. Divide the number by 10 to find remainder using modulus operator(%) .
2. Define reverse number variable and initialize 0 value
3. Multiply reverse number by 10 and add remainder to it.
4.Divide the number by 10.

Let's see Java code for this Problem.
  1. public class ReverseNumber {  
  2. public static void main(String[] args)  {  
  3. int number = 24586, reverse = 0;  
  4. while(number != 0{  
  5. int remainder = number % 10;  
  6. reverse = reverse * 10 + remainder;  
  7. number = number/10;  
  8. }  
  9. System.out.println("The reverse of the given number is: " + reverse);  
  10. }  
  11. }  

Given an array of ints, return true if the number of 1's is greater than the number of 4's

SDET

Let's understand the problem by following examples.

Ex1: [1, 4, 1] → true
Ex2: [1, 4, 1, 4] → false
Ex3: [1, 1] → true
Ex4: [1, 4, 4, 4] → false
Ex51: [1, 1, 1, 4] → true

Solution.

public class arrayProblem {

public static void main(String[] args) {

int [] a = {1,1,1,4,4};

int countof_1=0, countof_4=0;

for (int i=0; i<a.length; i++){

if (a[i]==1){

countof_1++;
}

else if (a[i]==4){

countof_4++;
}
}

if (countof_1>countof_4){

System.out.println(true);
}

else {

System.out.println(false);
}
}
}


Write Java Program to find sum of digits of given number.

SDET

 Let's we have a number 65847 so sum of digits of given number is 30(6+5+8+4+7). We are going to use following logic to solve this problem.

1. we'll break the number using modulus operator by using while loop.
2.Then add all digits.

  1. public class SumOfDigits{
  2. public static void main(String [] args){
  3. int number = 65847;
  4. int sum = 0, digit;
  5. while(number!=0){
  6. remainder = number%10;
  7. sum = sum + digit;
  8. number = number/10;
  9. }
  10. System.out.println("Sum of digits of the given number is "+sum); // 30
  11. }
  12. }