Petrol Pump solution Mettl.

Question :


A petrol tank has infinite capacity it can store infinite litres of petrol .

There are Four petrolpumps say 1 , 2 , 3 , 4. which are in circular manner.

PetrolTank should start from one petrol pump and should reach other petrolpump and should complete the circle with out any break.

Given input is a two dimensional array first column is petrol capacity of petrolpump and second column is distance to next petrol pump.

If petroltank can start from two petrol pumps means then you should start from least petrol pump number.

Given input array is int [][] array1 ={{3,3},
                            {7,4},
                            {7,8},
                            {9,8}}

For this input petrol tank can start from petrolpump 2 and petrolpump 4 but as we have to start from least number of petrolpump it will start from petrolpump 2 and will finish the circle.

Hints : You have to check whether input is two dimensional array or not .


Solution :

PetrolProblem.java

/**
 * 
 */
package com.techyield.blogspot.in;

/**
 * @author SATYA
 *
 */
public class PetrolProblem {


public int startPointofPetrolTank(int [][] input){
int petrolPumpnumber=0;
try{
int row= input.length;
int totaldistance=0;
int totalpetrol=0;
for(int i=0;i<row;i++){
int col=input[i].length;
if(col!=2){
throw new UnsupportedException("Input is not a two dimentional array.");
}
}
for(int i =0; i<row;i++){
totaldistance+=input[i][1];
totalpetrol+=input[i][0];
}
if(totalpetrol<totaldistance){
return 0;
}
for(int i =0; i<row;i++){
if(input[i][0]>input[i][1]){
petrolPumpnumber=i+1;
break;
}
}

}
catch(UnsupportedException e){
System.out.println(e.getMessage());
}
catch(Exception e){
System.out.println(e.getMessage());
}
return petrolPumpnumber;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int [][] array1 ={{3,3},
{7,4},
{7,8},
{9,8}
};
PetrolProblem object = new PetrolProblem();
int petrolPumpindex =object.startPointofPetrolTank(array1);
if(petrolPumpindex >=1)
System.out.println("Petrol Tank will start from PetrolPump "+petrolPumpindex+"");
else
System.out.println("PetrolTank can't start from any petrol pump");
}

}

UnsupportedException.java


package com.techyield.blogspot.in;

public class UnsupportedException extends Exception {

public UnsupportedException(String s){
super(s);
}

}


Output :

Petrol Tank will start from PetrolPump 2







Java quiz 1


Ques 1 : Which declaration of the main method below would allow a class to be started as a standalone program.


(A) public static int main(char args[])


(B) public static void main(String args[])


(C) public static void MAIN(String args[])


(D) public static void main(String args)


Answer : public static void main(String args[])

Ques 2 : What is the meaning of the return data type void?


(A) An empty memory space is returned so that the developers can utilize it.


(B) void is not supported in Java


(C) void returns no data type.


(D) null


Answer : void returns no data type.

Ques 3 : Which of these are legal identifiers.


(A) number_1


(B) number_a


(C) $1234


(D) All of the above.


Answer : All of the above.

Ques 4 : Which of the following are Java keywords?


(A) throw


(B) void


(C) private


(D) All of the above.


Answer : All of the above.

Ques 5 : A lower precision can be assigned to a higher precision value in Java. For example a byte type data can be assigned to int type.


(A) True


(B) False


Answer : True

Ques 6 : Which of these are not legal identifiers.


(A) 1alpha


(B) xy+abc


(C) both A and B


(D) None of the above


Answer : both A and B

Ques 7 : Which of the following are legal definitions of the main method that can be used to execute a class.


(A) public static int main(String args[])


(B) public void main(String args)


(C) public static void main(String args[])


(D) public static void main(string args[])


Answer : public static void main(String args[])

Ques 8 : Which of the following statements about the Java language is true?


(A) Both procedural and OOP are supported in Java.


(B) Java supports only procedural approach towards programming.


(C) Java supports only OOP approach.


(D) None of the above.


Answer : Both procedural and OOP are supported in Java.

Ques 9 : Which of the following are keywords in Java.


(A) implement


(B) friend


(C) NULL


(D) synchronized


Answer : synchronized

Ques 10 : Which of these are legal array declarations or definitions?


(A) int[] []x[];


(B) int x[5];


(C) int *x;


(D) None of above


Answer : int[] []x[];

Ques 11 : What gets printed when the following code is compiled and run with the following command - java test 2 Select the one correct answer.






public class test {


public static void main(String args[]) {


Integer intObj=Integer.valueOf(args[args.length-1]);


int i = intObj.intValue();






if(args.length > 1)


System.out.println(i);


if(args.length > 0)


System.out.println(i - 1);


else


System.out.println(i - 2);


}


}









(A) test


(B) test -1


(C) 0


(D) 1


Answer : 1


Description :Note that the program gets one command line argument - 2. args.length will get set to 1. So the condition if(args.length > 1) will fail, and the second check if(args.length > 0) will return true.

Ques 12 : Which of the following statements is false about objects?


(A) An instance of a class is an object


(B) Objects can access both static and instance data


(C) Objects do not permit encapsulation


(D) Object is the super class of all other classes


Answer : Objects do not permit encapsulation

Ques 13 : Which of these are legal identifiers. Select the three correct answers.


a. number_1


b. number_a


c. $1234


d. -volatile



(A) a, b, c


(B) a, b


(C) a


(D) b


Answer : a, b, c

Ques 14 : The class Hashtable is used to implement which collection interface. Select the one correct answer.


(A) List


(B) Set


(C) Map


(D) SortedSet


Answer : Map


Description :


The collection interface Map has two implementation HashMap and Hashtable.







Ques 15 : TreeMap class is used to implement which collection interface. Select the one correct answer.


(A) Set


(B) SortedSet


(C) Tree


(D) SortedMap


Answer : SortedMap

Ques 16 : Given a one dimensional array arr, what is the correct way of getting the number of elements in arr. Select the one correct answer.


(A) arr.length


(B) arr.length - 1


(C) arr.size


(D) arr.length()


Answer : arr.length

Ques 17 : What happens when the following code is compiled and run. Select the one correct answer.






for(int i = 1; i < 3; i++)


for(int j = 3; j > i; j--)


assert i!=j {System.out.println(i); }



(A) The class compiles and runs, but does not print anything.


(B) The number 1 gets printed with AssertionError


(C) The program generates a compilation error.


(D) The number 2 gets printed with AssertionError


Answer : The program generates a compilation error.


Description :The condition in assert statement must be followed by a semi-colon.

Ques 18 : What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.





protected class example {


public static void main(String args[]) {


String test = "abc";


test = test + test;


System.out.println(test);


}


}



(A) The class does not compile because the top level class cannot be protected.


(B) The program prints "abc"


(C) The program prints "abcabc"


(D) The program does not compile because statement "test = test + test" is illegal.


Answer : The class does not compile because the top level class cannot be protected.

Ques 19 : In the following class definition, which is the first line (if any) that causes a compilation error. Select the one correct answer.





public class test {


public static void main(String args[]) {


char c;


int i;


c = 'A'; // 1


i = c; //2


c = i + 1; //3


c++; //4


}


}



(A) The line labeled 1.


(B) The line labeled 2.


(C) The line labeled 3.


(D) All the lines are correct and the program compiles.


Answer : The line labeled 3.


Description :It is not possible to assign an integer to a character in this case without a cast.

Ques 20 : Which of the following is a Java keyword. Select the four correct answers.


a. extern


b. synchronized


c. volatile


d. friend


e. friendly


f. transient


g. this


h. then



(A) b, c


(B) b, c, f, g


(C) e, g, h


(D) all of above.


Answer : b, c, f, g

Area of a Parallelogram-Hacker Rank Problem.

You are given a class Solution with a main method. Your task is to complete the given code so that it outputs the area of a parallelogram with breadth  and height . You should read the variables from standard input.
If  or  , the output should be "java.lang.Exception: Breadth and height must be positive" without quotes.
Input Format
Two lines of input. First line contains : breadth of parallelogram. Next line contains : height of parallelogram.
Constraints 
 
Output Format
If both values are greater than zero, then the main method must output the area of the parallelogram; else, print "java.lang.Exception: Breadth and height must be positive" without quotes.
Sample input 1
1
3
Sample output 1
3
Sample input 2
-1
2
Sample output 2
java.lang.Exception: Breadth and height must be positive
Solution :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
//Write your code here
static boolean flag=false;
static int B,H;
static{
 Scanner in= new Scanner(System.in);
 B=in.nextInt();
 H=in.nextInt();
 try{
  if(B<=0||H<=0)
   throw new Exception("Breadth and height must be positive");
  else{
   flag= true;
  }
 }
 catch(Exception e){
  System.out.println(e);
 }
}                                                                   public static void main(String[] args){
  if(flag){
   int area=B*H;
   System.out.print(area);
  }
  
 }//end of main

}//end of class

Print The Array-Hacker Rank Problem.

Let's say you have an integer array and a string array. You have to write a SINGLE method printArray that can print all the elements of both arrays. The method should be able to accept both integer arrays or string arrays.
You are given code in the editor. You have to complete it so that it prints the following lines:
1
2
3
Hello
World
You should not use method overloading (your answer will not get accepted).
Solution 1: For printArray method :

import java.io.IOException;
import java.lang.reflect.Method;
class Printe
{
//Write your code here
class Printer
{
//Write your code here
public void printArray(Object[] ar){
for(int i=0;i<ar.length;i++){
System.out.println(ar[i].toString());
}
}
}
}

Solution 2 : For printArray Method :
import java.io.IOException;
import java.lang.reflect.Method;

class Printer
{
//Write your code here
class Printer
{
//Write your code here
public void printArray(Object[] ar){
for(Object obj: ar){
if(obj instanceof String || obj instanceof Integer){
System.out.println(obj.toString());
}

                    }
}


}


}

// Already Provided code by them for testing printArray Method.

public class Solution
{
    

    public static void main( String args[] )
    {
        Printer myPrinter=new Printer();
        Integer[] intArray = { 1, 2, 3 };
        String[] stringArray = {"Hello","World"};
        myPrinter.printArray( intArray  );
        myPrinter.printArray( stringArray );
        int count=0;
      for (Method method : Printer.class.getDeclaredMethods()) {
            String name = method.getName();
            if(name.equals("printArray"))
              count++;
        }
        
        if(count>1)System.out.println("Method overloading is not allowed!");
        assert count==1;

    } 
}






Weird Numbers solution HackerRank

Given an integer  as input, can you check the following:
  • If  is odd then print "Weird"
  • If  is even and, in between range 2 and 5(inclusive), print "Not Weird"
  • If  is even and, in between range 6 and 20(inclusive), print "Weird"
  • If  is even and , print "Not Weird"
Input Format
Single line of input: integer .
Constraints
Output Format
Print "Weird" if the number is weird; else, not "Not Weird" without the quotes.
Sample Input 1
3
Sample Output 1
Weird
Sample Input 2
24
Sample Output 2
Not Weird
Solution:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

 public static void main(String[] args) {
  /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
  Scanner in = new Scanner(System.in);
  int i = in.nextInt();
  if(i>=1&&i<=100){// Checking condition of 1<=N<=100
   if(!(i%2==0))//Checking if N is odd printing Weird.
   {
    System.out.println("Weird");
   }
   else if(i%2==0){
    if((i>=2&&i<=5)||i>20)
     System.out.println("Not Weird");
    if(i>=6&&i<=20)
     System.out.println("Weird");
   }
  }
 }    
}

Featured Post

H1B Visa Stamping at US Consulate

  H1B Visa Stamping at US Consulate If you are outside of the US, you need to apply for US Visa at a US Consulate or a US Embassy and get H1...