Rabu, 06 Juli 2016

Java interview programming questions on this keyword part 2



Java Quiz on this keyword part #2
  • Java interview program on this keyword: can we call method using this keyword?
  
Program  #5:

java programming on this keyword





Program #6: Can we call non static method from constructor using this?

Java programming examples on this keyword





Program #7: Can we assign something to this ?



  1. package thiskeywordinterviewprograms.java;
  2. public class ThisDemo {
  3.  
  4.     int a;
  5.     int b;
  6.     
  7.  ThisDemo(int x, int y){
  8.  
  9.        this= new ThisDemo();
  10.         System.out.println("Two argument constructor called.");
  11.         this.x=x;
  12.         this.y=y;
  13.         
  14. }
  15.  
  16. public static void main(String[] args) {
  17.         
  18.     ThisDemo obj = new ThisDemo(10, 20);
  19.         
  20.      System.out.println(obj.a);
  21.      System.out.println(obj.b);

  22. }
  23.  
  24. }





Program #8 : Can we use this as return statement in a method?


  1. public class B{

  2.    int a;
  3.     
  4.  public int getA() {
  5.         return a;
  6.  }
  7.  
  8. public void setA(int a) {
  9.         this.a = a;
  10. }
  11.  
  12. B show(){
  13.     return this;
  14. }
  15.  
  16. public static void main(String[] args) {
  17.        
  18.  B obj = new B();
  19.  
  20.   obj.setA(10);
  21.  
  22.  System.out.println(obj.getA());
  23.  B obj2= obj.show();
  24.  System.out.println(obj2.getA());
  25.  
  26. }

  27. }





Baca selengkapnya

Java Interview programming questions on this keyword

  • Lets discuss some interesting programs on this keyword.
  • Now its time to practice some programs on this keyword. 
  • Try to guess the output of the following java programming questions on this keyword.
  • If you want explanations for all the programs please go through this keyword interview questions on java
Java Quiz on this keyword part #1



Program #1: Java interview programming question on this keyword.


  1. package thiskeywordinterviewprograms.java;
  2. public class ThisDemo {
  3.  
  4.     int a;
  5.     int b;
  6.     
  7. ThisDemo(int a, int b){
  8.  
  9.     a=a;
  10.     b=b;
  11.         
  12. }
  13.  
  14. public static void main(String[] args) {
  15.         
  16.     ThisDemo obj = new ThisDemo(10, 20);
  17.         
  18.      System.out.println(obj.a);
  19.      System.out.println(obj.b);

  20. }
  21.  
  22. }





Program #2: Java interview programming question on this keyword.

  1. package thiskeywordinterviewprograms.java;
  2. public class ThisDemo {
  3.  
  4.     int a;
  5.     int b;
  6.     
  7. ThisDemo(int a, int b){
  8.  
  9.     this.a=a;
  10.     this.b=b;
  11.         
  12. }
  13.  
  14. public static void main(String[] args) {
  15.         
  16.     ThisDemo obj = new ThisDemo(10, 20);
  17.         
  18.      System.out.println(obj.a);
  19.      System.out.println(obj.b);

  20. }
  21.  
  22. }




Program #3: Java Interview programming example on this keyword.

  • What will be the output of the below program

  1. package thiskeywordinterviewprograms.java;
  2. public class ThisDemo {
  3.  
  4.     int a;
  5.     int b;
  6.     
  7. ThisDemo(int a, int b){
  8.  
  9.     this.a=a;
  10.     this.b=b;
  11.         
  12. }
  13.  
  14. public static void main(String[] args) {
  15.         
  16.     ThisDemo obj = new ThisDemo();
  17.         
  18.      System.out.println(obj.a);
  19.      System.out.println(obj.b);

  20. }
  21.  
  22. }





Program #4: Java interview programming question on this keyword.


java programming examples on this keyword




  1. Zero argument constructor called.
  2. Two argument constructor called.
  3. x=1
  4. y=2

Interview Programming questions on this keyword part 2
Baca selengkapnya

Selasa, 05 Juli 2016

Java Program to find shortest palindrome in string

  • We have a Letter or a word then we need add some letters to it and need to find out shortest palindrome 
  • For example we take "S":  S will be the shortest palindrome string.
  • If we take "xyz"zyxyz will be the shortest palindrome string
  • So we need to add some characters to the given string or character and find out what will be the shortest palindrome string by using simple java program.


Java example Program to find out shortest palindrome of given string


  1. package shortestpalindromeexample.java;
  2. import java.util.Scanner;
  3.  
  4. public class ShortestPalindromeDemo {
  5.  
  6. public static String shortestPalindrome(String str) {
  7.      
  8. int x=0;  
  9. int y=str.length()-1;
  10.      
  11.   while(y>=0){
  12.      if(str.charAt(x)==str.charAt(y)){
  13.           x++;
  14.          }
  15.             y--;
  16.   }
  17.  
  18. if(x==str.length())
  19. return str;
  20.  
  21. String suffix = str.substring(x);
  22. String prefix = new StringBuilder(suffix).reverse().toString();
  23. String mid = shortestPalindrome(str.substring(0, x));
  24.  
  25. return prefix+mid+suffix;
  26. }
  27.  
  28. public static void main(String[] args) {
  29.  
  30. Scanner in = new Scanner(System.in);
  31.  
  32. System.out.println("Enter a String to find out shortest palindrome");
  33.  
  34. String str=in.nextLine();
  35.  
  36. System.out.println("Shortest palindrome of "+str+" is "+shortestPalindrome(str));
  37.  
  38. }
  39.  
  40. }
Output:

  1. Enter a String to find out shortest palindrome
  2. java
  3. Shortest palindrome of java is avajava

find shortest palindrome in java program

Baca selengkapnya