• Slider Banner

    Prepare for

    Technical Interview

    Over .1 Million+ Interview Questions for different profiles

    Read More
  • Slider Banner

    Join the

    Programming World

    Learn about top programming languages, tools and frameworks.

    Read More
  • Slider Banner

    Prepare with

    Certification MCQ

    Practice for certification from 1000+ multiple choice questions.

    Read More

Given a string of even length, return the first half. So the string "WooHoo" yields "Woo".

Check below code:

package com.test.strings;

public class FindStringFirstHalf 
{
	public static void main(String args[])
	{
		FindStringFirstHalf fh = new FindStringFirstHalf();
		System.out.println(fh.firstHalf("WooHoo"));
		System.out.println(fh.firstHalf("HelloThere"));
	}
	
	public String firstHalf(String str) 
	{
	  return str.substring(0, str.length()/2);	
	}
}

Output

Woo
Hello

What is object-oriented paradigm?

It is a programming paradigm based on objects having data and methods defined in the class to which it belongs. Object-oriented paradigm aims to incorporate the advantages of modularity and reusability. Objects are the instances of classes which interacts with one another to design applications and programs. There are the following features of the object-oriented paradigm.

  • Follows the bottom-up approach in program design.
  • Focus on data with methods to operate upon the object's data
  • Includes the concept like Encapsulation and abstraction which hides the complexities from the user and show only functionality.
  • Implements the real-time approach like inheritance, abstraction, etc.
  • The examples of the object-oriented paradigm are C++, Simula, Smalltalk, Python, C#, etc.

What will be the output of below java code?

Example Code:

class ForWith0Comparison   
{
    public static void main (String args[])
    {
        for(int i=0; 0; i++)
        {
            System.out.println("Hello ProgramsBuzz");
        }  
    }  
} 

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from int to boolean

	at com.test.controlflowstatements.ForWith0Comparison.main(ForWith0Comparison.java:7)

What will be the output of the following Java program?

Example Code:

package com.test.strings;

class StringIntMultiply   
{
    public static void main (String args[])
    {
    	System.out.println(10 * 20 + "programsbuzz");
    	System.out.println("programsbuzz" + 10 * 20);  
    }  
}  

Output

200programsbuzz
programsbuzz200

What is the purpose of static methods and variables?

The methods or variables defined as static are shared among all the objects of the class. The static is the part of the class and not of the object. The static variables are stored in the class area, and we do not need to create the object to access such variables. Therefore, static is used in the case, where we need to define variables or methods which are common to all the objects of the class.

For example, In the class simulating the collection of the students in a college, the name of the college is the common attribute to all the students. Therefore, the college name will be defined as static.

How to do JSON Schema Validation using Rest Assured?

Use below steps for JSON schema validation using Rest Assured

Step 1: Copy Latest Maven dependency from below link

https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator

Step 2: Get JSON Schema from developer or generate using some tool (Click Here for online Tool) from your api. Use below sample for test (save it with Sample.json):

{
  "$schema": "http://json-schema.org/draft-04/schema",
  "type": "array",
  "items": [
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    },
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    },
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    },
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    },
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    }
  ]
}

Step 3: Create Sample.java file and paste below code

import org.testng.annotations.Test;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;

import io.restassured.RestAssured;

public class Sample
{
	@Test
	
	public void testJsonSchema()
	{
		RestAssured.given()
			.when()
			.get("https://jsonplaceholder.typicode.com/comments?postId=1")
			.then()
			.assertThat()
			.body(matchesJsonSchemaInClasspath("com/mycompany/api/bl/Sample.json"));
	}
}

Here in get you have to specify your request and in body you have to specify path where your json schema file is.

Subscribe to