Author Topic: Java quiz series  (Read 23636 times)

0 Members and 7 Guests are viewing this topic.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #45 on: November 28, 2013, 09:04:48 pm »
Ah well I did my research this time.
A double means double-precision, containing 64 bits while a float contains 32.
1/10 can't be represented by a binary fraction so the computer scales the answer down to the closest fraction it can get legitimately.
If I'm incorrect again would you mind shooting me a pm containing the correct explanation? Thanks.

That one is correct. I will post the next quiz tomorrow or someone else who has one can post it too. Gtg
« Last Edit: November 28, 2013, 09:05:07 pm by Deque »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #46 on: November 29, 2013, 07:30:45 pm »
Is the following a valid statement? Why?

Code: (Java) [Select]
String [] foo [] = null;

Offline jeyd

  • NULL
  • Posts: 3
  • Cookies: 0
    • View Profile
Re: Java quiz series
« Reply #47 on: December 02, 2013, 01:08:51 pm »
I think it is valid. It is two dimensional String.
              String[][] foo         &&          String[] foo []           &&         String foo [][]         
 all are the same..
  I don't have any new quiz if I'm right.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #48 on: December 02, 2013, 02:42:03 pm »
Correct.

Code: (Java) [Select]
public class Home {
   
   private static Home home = new Home();
   private static int DEFAULT_NR_OF_ROOMS = 4;
   private final int rooms;
   
   public Home() {
      rooms = DEFAULT_NR_OF_ROOMS - 1;
   }

   public static void main(String[] args) {
      System.out.println(home.rooms);
   }

}

What does this print and why?
« Last Edit: December 04, 2013, 01:28:52 pm by Deque »

Offline jeyd

  • NULL
  • Posts: 3
  • Cookies: 0
    • View Profile
Re: Java quiz series
« Reply #49 on: December 02, 2013, 04:28:02 pm »
Correct.

Code: (Java) [Select]
public class Home {
   
   private static Home home = new Home();
   private static int DEFAULT_NR_OF_ROOMS = 4;
   private final int rooms;
   
   public Home() {
      rooms = DEFAULT_NR_OF_ROOMS - 1;
   }

   public static void main(String[] args) {
      System.out.println(home.rooms);
   }

}

What does this print and why?

 Well the problem is with int rooms because it is final and at declaration it is not assigned any value. Final variables must be assigned values at their declaration.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #50 on: December 02, 2013, 08:33:21 pm »
Well the problem is with int rooms because it is final and at declaration it is not assigned any value. Final variables must be assigned values at their declaration.

Nope. It is totally fine to initialize final fields in the constructor and if you don't init final fields properly the compiler will complain. But this code compiles and runs without a problem.
The question is: Why this output?

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Java quiz series
« Reply #51 on: December 03, 2013, 10:49:50 am »
It outputs 3.

The home member object was used inside the class so access specifiers are irrelevant.

fun fact: each home object has a static home object within it that can be referenced infinitely so this is also a legal statement
Code: (java) [Select]
System.out.println(home.home.home.home.home.rooms); //homes inside of homes inside of homes. its Homeception

The Bigger they are...The more likely you'll get your ass kicked

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #52 on: December 03, 2013, 07:47:42 pm »
It outputs 3.

The home member object was used inside the class so access specifiers are irrelevant.

Wrong.
Run the code and reconsider your answer.

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Java quiz series
« Reply #53 on: December 03, 2013, 08:58:05 pm »
Wrong.
Run the code and reconsider your answer.

Well, it still outputs 3. was that part at least right?
The Bigger they are...The more likely you'll get your ass kicked

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #54 on: December 04, 2013, 01:24:19 pm »
Well, it still outputs 3. was that part at least right?

It does not output 3. What VM are you using?

Edit: Ok, I overlooked a part in the JVM specification about final fields (I didn't know that putting in final would change it and the code I put here first was working the way I wanted to). Try now with the code above.
« Last Edit: December 04, 2013, 01:32:28 pm by Deque »

Offline xmajoh

  • NULL
  • Posts: 2
  • Cookies: 17
    • View Profile
Re: Java quiz series
« Reply #55 on: December 04, 2013, 02:36:49 pm »
It outputs -1

You are calling constructor before you assign the value to the static variable. So in the constructor code, there no value of static variable.
« Last Edit: December 04, 2013, 02:40:16 pm by xmajoh »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #56 on: December 04, 2013, 02:41:08 pm »
It outputs -1

You are calling constructor before you assign the value to the static variable. So in the constructor code, there no value of static variable.

Correct. Reason can be found here at step 9
http://docs.oracle.com/javase/specs/jls/se5.0/html/execution.html#12.4.2
It also explains why it didn't work with final. Final fields are initialized before the others.

You can make the next quiz.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #57 on: December 04, 2013, 07:20:56 pm »
Next quiz, same question. What will be printed and why?

Code: (Java) [Select]
import java.util.HashSet;
import java.util.Set;

public class Name {
private final String first, last;

public Name(String first, String last) {
this.first = first;
this.last = last;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof Name))
return false;
Name n = (Name) o;
return n.first.equals(first) && n.last.equals(last);
}

public static void main(String[] args) {
Set<Name> s = new HashSet<Name>();
s.add(new Name("Mickey", "Mouse"));
System.out.println(s.contains(new Name("Mickey", "Mouse")));
}
}

Offline red.evil

  • /dev/null
  • *
  • Posts: 16
  • Cookies: 1
    • View Profile
Re: Java quiz series
« Reply #58 on: January 02, 2014, 02:43:34 am »
It returns false.
Because we're using an instance of HashSet<> as Set; and this HashSet uses the methods hashCode() and equals() to check if two items given to the Set are equal or not.
Since we haven't implemented an extra hashCode method, it uses the standart hashCode() from Object and there we will get two different codes for those two objects.

// Edit: added the explanation

Example to get true:
Code: (java) [Select]
   
import java.util.HashSet;
import java.util.Set;
     
public class Name
{
      private final String first, last;
   
      public Name( String first, String last )
      {
            this.first = first;
            this.last = last;
       }
     
        @Override
        public boolean equals( Object o )
        {
              if ( !( o instanceof Name ) )
                      return false;

              Name n = (Name) o;
              return n.first.equals( first ) && n.last.equals( last );
        }
       
        @Override
        public int hashCode()
        {
             return( first.hashCode() + last.hashCode() ); // this isn't a good hashCode method! Don't do it in real but here it works
        }
     
        public static void main( String[] args )
       {
              Set<Name> s = new HashSet<Name>();
               s.add( new Name( "Mickey", "Mouse" ) );
              System.out.println( s.contains( new Name( "Mickey", "Mouse" ) ) );
        }
}
« Last Edit: January 02, 2014, 02:48:10 am by red.evil »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #59 on: January 02, 2014, 08:57:13 am »
It returns false.
Because we're using an instance of HashSet<> as Set; and this HashSet uses the methods hashCode() and equals() to check if two items given to the Set are equal or not.
Since we haven't implemented an extra hashCode method, it uses the standart hashCode() from Object and there we will get two different codes for those two objects.

// Edit: added the explanation

Example to get true:
Code: (java) [Select]
   
import java.util.HashSet;
import java.util.Set;
     
public class Name
{
      private final String first, last;
   
      public Name( String first, String last )
      {
            this.first = first;
            this.last = last;
       }
     
        @Override
        public boolean equals( Object o )
        {
              if ( !( o instanceof Name ) )
                      return false;

              Name n = (Name) o;
              return n.first.equals( first ) && n.last.equals( last );
        }
       
        @Override
        public int hashCode()
        {
             return( first.hashCode() + last.hashCode() ); // this isn't a good hashCode method! Don't do it in real but here it works
        }
     
        public static void main( String[] args )
       {
              Set<Name> s = new HashSet<Name>();
               s.add( new Name( "Mickey", "Mouse" ) );
              System.out.println( s.contains( new Name( "Mickey", "Mouse" ) ) );
        }
}

Correct. It's your turn now. Post a quiz.