Author Topic: Java quiz series  (Read 23560 times)

0 Members and 2 Guests are viewing this topic.

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: Java quiz series
« Reply #30 on: November 16, 2013, 06:09:44 pm »
Both sufficient, but with particular notice to Neea. You'd especially want to go by significant figures if in finance.

Take the next one, Neea.
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline Neea

  • Peasant
  • *
  • Posts: 83
  • Cookies: 19
  • Laboris Gloria Ludi
    • View Profile
Re: Java quiz series
« Reply #31 on: November 16, 2013, 07:52:38 pm »
Say you have the following classes
Code: (java) [Select]

public abstract class Symbol {
private char symbol;
......... //constructor

@Override
public String toString() {
return new Character(symbol).toString();
}
}

public class Nonterminal extends Symbol {
        ........ //constructors

 @Override
public String toString() {
return super.toString();
}
}
public class Terminal extends Symbol {
        ..... //constructors

@Override
public String toString() {
return super.toString();
}
}


Will this compile or not, will it print out "it works" or not and why, and how would you make it work?


Code: (java) [Select]
......
Nonterminal nt = new Nonterminal("S");
Terminal st = new Terminal("S");

if(nt.equals(st)){
    System.out.println("It works");
}
.....


Not a particularly hard question, but oh well .... my java knowledge is fairly limited.
<!--Here be Cthulhu -->

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #32 on: November 18, 2013, 09:04:40 am »
Say you have the following classes
Code: (java) [Select]

public abstract class Symbol {
   private char symbol;
   ......... //constructor

@Override
   public String toString() {
      return new Character(symbol).toString();
   }
}

public class Nonterminal extends Symbol {
        ........ //constructors

 @Override
   public String toString() {
      return super.toString();
   }
}
public class Terminal extends Symbol {
        ..... //constructors

   @Override
   public String toString() {
      return super.toString();
   }
}


Will this compile or not, will it print out "it works" or not and why, and how would you make it work?


Code: (java) [Select]
......
Nonterminal nt = new Nonterminal("S");
Terminal st = new Terminal("S");

if(nt.equals(st)){
    System.out.println("It works");
}
.....


Not a particularly hard question, but oh well .... my java knowledge is fairly limited.

No one here has been trying to answer it by now, so I will to keep this series going.

It will compile (assumed that the code parts not given are correct).
It won't print "it work's". The reason is the default implementation of equals:

Code: (Java) [Select]
public boolean equals(Object obj) {
        return (this == obj);
 }

The == will only be true if the memory addresses of the objects are the same. In this case they aren't.
In order to make it work you would have to override equals in Symbol.

Example implementation:

Code: (Java) [Select]
@Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (obj instanceof Symbol) {
         Symbol other = (Symbol) obj;
         if (symbol != other.symbol)
            return false;
      }
      return true;
   }

//if you change equals you have to change hashCode too
@Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + symbol;
      return result;
   }


But: It is a bad idea to make objects with two different subtypes equal to each other. They wouldn't need different types if they where equal.
« Last Edit: November 18, 2013, 09:07:12 am by Deque »

Offline Neea

  • Peasant
  • *
  • Posts: 83
  • Cookies: 19
  • Laboris Gloria Ludi
    • View Profile
Re: Java quiz series
« Reply #33 on: November 18, 2013, 09:15:06 am »
Your answer is correct, but if you need to compare the 2 values of the different objects, which was the case in my example you don't need to override the equals method, just use toString() and compare both as string objects. This was the reason why i let the toString() methods visible in my example. Please continue :)
<!--Here be Cthulhu -->

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #34 on: November 18, 2013, 02:18:27 pm »
Your answer is correct, but if you need to compare the 2 values of the different objects, which was the case in my example you don't need to override the equals method, just use toString() and compare both as string objects. This was the reason why i let the toString() methods visible in my example. Please continue :)

Ah, I didn't know that I can touch this part of the code, because then you could just do

Code: [Select]
System.out.println("It works");
And it is alright too.

However, next quiz:

Code: (Java) [Select]
... x = ...
if (x != x) System.out.println("Hello Evilzone");

What do you have to put into the ... to make it print out "Hello Evilzone"?

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Java quiz series
« Reply #35 on: November 23, 2013, 08:08:01 am »
 
Code: (java) [Select]
double x =  Double.NaN;
if (x != x) System.out.println("Hello Evilzone");

you use a NaN (not a number)which is an undefined value. NaN can not equal NaN because many different operations can result in NaN such as 0 divided by 0,or log(-1).

the double class has a built-in static NaN value use here.

i can't think of anything to use for a quiz at the moment.
« Last Edit: November 23, 2013, 08:50:47 am by Super_mario666 »
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 #36 on: November 23, 2013, 10:10:07 am »
Code: (java) [Select]
double x =  Double.NaN;
if (x != x) System.out.println("Hello Evilzone");

you use a NaN (not a number)which is an undefined value. NaN can not equal NaN because many different operations can result in NaN such as 0 divided by 0,or log(-1).

the double class has a built-in static NaN value use here.

i can't think of anything to use for a quiz at the moment.

Correct



Next quiz:

Code: (Java) [Select]
long l = 1000000000 + 2000000000;
System.out.println(l);

What is the value of l and why?

Offline Snayler

  • Baron
  • ****
  • Posts: 812
  • Cookies: 135
    • View Profile
Re: Java quiz series
« Reply #37 on: November 23, 2013, 02:30:45 pm »
3.000.000.000, because l is of type long, which can support numbers from -2^63 to (2^63)-1 [signed].
If it was an int, it couldn't possibly hold the end result of the operation since an int can hold a maximum value of around 2 billion.
« Last Edit: November 23, 2013, 02:35:47 pm by Snayler »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #38 on: November 23, 2013, 04:59:41 pm »
3.000.000.000, because l is of type long, which can support numbers from -2^63 to (2^63)-1 [signed].
If it was an int, it couldn't possibly hold the end result of the operation since an int can hold a maximum value of around 2 billion.

Run it and reconsider your answer. ;)

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Java quiz series
« Reply #39 on: November 25, 2013, 05:27:21 am »
it is definitely strange. it being cast as an int for some reason. :-\   
The Bigger they are...The more likely you'll get your ass kicked

Offline Snayler

  • Baron
  • ****
  • Posts: 812
  • Cookies: 135
    • View Profile
Re: Java quiz series
« Reply #40 on: November 25, 2013, 05:38:57 am »
Next quiz:

Code: (Java) [Select]
long l = 1000000000 + 2000000000;
System.out.println(l);

What is the value of l and why?
The value of "l" is -1294967296.
This happens because the arithmetic operation is being done with two int numbers. Assigning the result to a long variable is irrelevant. In this case, the answer would be out of bound for an int, so it returns this strange value.

The fix:
Code: (Java) [Select]
long l = 1000000000;
l += 2000000000;
System.out.println(l);



I have no quiz
« Last Edit: November 25, 2013, 05:44:24 am by Snayler »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #41 on: November 25, 2013, 09:18:14 pm »
The value of "l" is -1294967296.
This happens because the arithmetic operation is being done with two int numbers. Assigning the result to a long variable is irrelevant. In this case, the answer would be out of bound for an int, so it returns this strange value.

The fix:
Code: (Java) [Select]
long l = 1000000000;
l += 2000000000;
System.out.println(l);



I have no quiz

Correct.

An alternative fix:
Code: (Java) [Select]
long l = 1000000000 + 2000000000L;
System.out.println(l);



Next quiz:

Code: (Java) [Select]
double c = 0.1;
if (c * 0.1 == 0.01) {
   System.out.println("Hell yeah.");
} else {
   System.out.println("Nooooo.");
}

What does it print and why?
« Last Edit: November 25, 2013, 09:19:03 pm by Deque »

Offline Matriplex

  • Knight
  • **
  • Posts: 323
  • Cookies: 66
  • Java
    • View Profile
Re: Java quiz series
« Reply #42 on: November 25, 2013, 10:22:57 pm »
Next quiz:

Code: (Java) [Select]
double c = 0.1;
if (c * 0.1 == 0.01) {
   System.out.println("Hell yeah.");
} else {
   System.out.println("Nooooo.");
}

What does it print and why?

It would print "Nooooo.".
The equation would result in a very large amount of zeroes, and most calculators or other things would just round it up to 0.01, however because this is a double it goes the smallest a double can go, being

Code: (Java) [Select]
.010000000000000002

At least this is my interpretation of it, I may be wrong.

                                                                                                       

If I'm correct, I don't have a quiz.
\x64\x6F\x75\x65\x76\x65\x6E\x00

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #43 on: November 26, 2013, 02:08:35 pm »
It would print "Nooooo.".
The equation would result in a very large amount of zeroes, and most calculators or other things would just round it up to 0.01, however because this is a double it goes the smallest a double can go, being

Code: (Java) [Select]
.010000000000000002

At least this is my interpretation of it, I may be wrong.

                                                                                                       

If I'm correct, I don't have a quiz.

The explanation is not correct.

Offline Matriplex

  • Knight
  • **
  • Posts: 323
  • Cookies: 66
  • Java
    • View Profile
Re: Java quiz series
« Reply #44 on: November 26, 2013, 03:57:45 pm »
The explanation is not correct.
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.
\x64\x6F\x75\x65\x76\x65\x6E\x00