Posts

Showing posts with the label puzzle

Do you know Javascript … Try these puzzle

Do you know Javascript … Try these puzzle Javascript is one the most miss-understood language among developers. Everyday I learn new lesson about it.While going through few articles I thought of following JS language puzzles. Handling Array var days = [ 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' ]; var length = days. length ; console. log ( 'No of days are ' + days. length ); //Opps we missed Sunday, lets add it days[++ length ] = 'Sunday' ; console. log ( 'Final no of days are ' + days. length ); It is obvious that no of days are 7…. is it ? Written with StackEdit . StackEdit is a full-featured, open-source Markdown editor based on PageDown, the Markdown library used by Stack Overflow and the other Stack Exchange sites. ↩ Here is the text of the footnote . ↩

Java Puzzle : Function parameters

Here is another Java puzzle, but this time you don’t need to find output of this program. Instead just complete the code snippet to get desired output. Here is the program : public class OPuzzle { public static void main(String[] args) { Object o = null; printMe(o); } private static void printMe(<Provide the declaration here> o){ if (o!=null){ System.out.println( "I am not null" ); } } } Desire output : I am not null Replace the <Provide the declaration here> with appropriate declaration to get above desired output. Leave your comments. Reference : http://google-opensource.blogspot.com/2011/04/geek-time-with-josh-bloch.html   No no no … don’t see reference before solving :).

Java Puzzle : Integer Initialization

Here is a simple program which instantiate Integer object using primitive int values with all possible ways, new operator, AutoBoxing , Integer.valueOf() . 1: package com.test; 2:   3: public class IntegerPuzzle { 4: public static void main(String[] args) { 5: Integer a = new Integer(100); 6: Integer b = new Integer(100); 7: Integer c = 100; 8: Integer d = 100; 9: Integer e = Integer.valueOf(100); 10: Integer f = 150; 11: Integer g = 150; 12: 13: System.out.println( "a = b ? " + (a==b)); 14: System.out.println( "c = d ? " + (c==d)); 15: System.out.println( "d = e ? " + (d==e)); 16: System.out.println( "a = c ? " + (a==c)); 17: System.out.println( "f = g ? " + (f==g)); 18: } 19: } What would be the output of  this program ?

Java Puzzle : String concatenation

I have simple program to print date after month from now. 1: package com.test; 2:   3: import java.text.ParseException; 4: import java.text.SimpleDateFormat; 5: import java.util.Date; 6:   7: public class Test{ 8: public static void main(String[]args) throws ParseException{ 9: Date currentDate = new Date(); 10: int d = currentDate.getDate(); 11: int month = currentDate.getMonth()+1; 12: int year = currentDate.getYear() +1900; 13: //trying to update to next moth same date 14: Date nextMonthDate = new SimpleDateFormat( "dd-MM-yyyy" ) 15: .parse(d + "-" 16: + month+1 + "-" // increasing month by one to get next month 17: + year); 18: System.out.println( "Current Date = " + currentDate); 19: System.out.println( "Next Month Date = " +nextMonth...

Java Puzzle–Immutable String

what will be output of the following program ? 1: class StringTest2 2: { 3: public static void main(String[] args) 4: { 5: String str1 = " Hello "; 6: String str2 = " HELLO "; 7: String str3 = str1.toUpperCase(); 8: String str4 = str2.toUpperCase(); 9: System.out.println(str1 == str3); 10: System.out.println(str2 == str4); 11: } 12: }

Java Puzzle – Magic of Auto-boxing

  Auto-boxing is introduced in JDK 5.0.  it converts primitive data variable to corresponding wrapper classes and vice versa without any explicit efforts. For example int to Integer and Integer to int . Here is the one program using auto-boxing feature 1: class AutoBoxingTest 2: { 3: public static void main(String[] args) 4: { 5: boolean b1= true , b2= true ; 6: System.out.println(" boolean Result = "+ compare(b1,b2)+" \n "); 7: System.out.println(" Int Result = "+ compare(10,10)+" \n "); 8: System.out.println(" String Result = "+ compare(" ABC "," ABCE ")+" \n "); 9: System.out.println(" float Result = "+ compare(10.00f,10f)+" \n "); 10: System.out.println(" Mixed Result = "+ compare(10l,10l)+" \n "); 11: System.out.println(" Mixed[long,int...

Java Puzzle – Method Overloading

Program - public class OverloadingPuzzle {     public void display(Object obj){         System.out.println("Object version");     }     public void display(String str){         System.out.println("String Version");     }     public static void main(String[] args){         OverloadingPuzzle obj = new OverloadingPuzzle();         obj.display(null);     } } I was expecting that this program prints “ Object Version ” but …. Output String Version Why ? – Searching ….