Author Topic: Java quiz series  (Read 23556 times)

0 Members and 2 Guests are viewing this topic.

Offline Mordred

  • Knight
  • **
  • Posts: 360
  • Cookies: 135
  • Nvllivs in Verba
    • View Profile
Re: Java quiz series
« Reply #15 on: November 14, 2013, 05:14:41 pm »
The Default access level means that it can be accessed only from within the same package. That is different from public, which can be accessed from outside the package as well.
« Last Edit: November 14, 2013, 05:16:13 pm by Mordred »
\x57\x68\x79\x20\x64\x69\x64\x20\x79\x6f\x75\x20\x65\x76\x65\x6e\x20\x66\x75\x63\x6b\x69\x6e\x67\x20\x73\x70\x65\x6e\x64\x20\x74\x68\x65\x20\x74\x69\x6d\x65\x20\x74\x6f\x20\x64\x65\x63\x6f\x64\x65\x20\x74\x68\x69\x73\x20\x6e\x69\x67\x67\x72\x3f\x20\x44\x61\x66\x75\x71\x20\x69\x73\x20\x77\x72\x6f\x6e\x67\x20\x77\x69\x74\x68\x20\x79\x6f\x75\x2e

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #16 on: November 14, 2013, 06:25:50 pm »
The Default access level means that it can be accessed only from within the same package. That is different from public, which can be accessed from outside the package as well.

How is that access level called?

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: Java quiz series
« Reply #17 on: November 14, 2013, 06:44:35 pm »
How is that access level called?

protected
Easter egg in all *nix systems: E(){ E|E& };E

Offline Snayler

  • Baron
  • ****
  • Posts: 812
  • Cookies: 135
    • View Profile
Re: Java quiz series
« Reply #18 on: November 14, 2013, 07:06:54 pm »
How is that access level called?
package-private

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #19 on: November 14, 2013, 07:13:36 pm »
package-private

Correct.
Well, now Mordred and you gave one half of the solution each. So one of you is free to make the next quiz. (Or I do it)

Offline Mordred

  • Knight
  • **
  • Posts: 360
  • Cookies: 135
  • Nvllivs in Verba
    • View Profile
Re: Java quiz series
« Reply #20 on: November 14, 2013, 07:25:09 pm »
noooo, I didn't make it in time to answer with the name :(

But maybe I have one:

Quote
While testing some code that you are developing, you notice that an ArrayIndexOutOfBoundsException is thrown. What is the appropriate reaction?
1. Enclose the offending code in a try block, with a catch block for ArrayIndexOutOfBoundsException that does nothing.
2. Enclose the offending code in a try block, with a catch block for ArrayIndexOutOfBoundsException that prints out a descriptive message.
3. Declare that the method that contains the offending code throws ArrayIndexOutOfBoundsException.
4. None of the above.

« Last Edit: November 14, 2013, 07:25:49 pm by Mordred »
\x57\x68\x79\x20\x64\x69\x64\x20\x79\x6f\x75\x20\x65\x76\x65\x6e\x20\x66\x75\x63\x6b\x69\x6e\x67\x20\x73\x70\x65\x6e\x64\x20\x74\x68\x65\x20\x74\x69\x6d\x65\x20\x74\x6f\x20\x64\x65\x63\x6f\x64\x65\x20\x74\x68\x69\x73\x20\x6e\x69\x67\x67\x72\x3f\x20\x44\x61\x66\x75\x71\x20\x69\x73\x20\x77\x72\x6f\x6e\x67\x20\x77\x69\x74\x68\x20\x79\x6f\x75\x2e

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: Java quiz series
« Reply #21 on: November 14, 2013, 07:31:46 pm »
It is fallacious to blindly follow some sort of archetypal convention in the face of a given scenario, especially with something as general and out of context as an exception.

An ArrayIndexOutOfBoundsException could be thrown as a result of poor and/or rudimentary argument parsing (assigning variables to args[n]) and not specifying an option when running the program.

Depending on your situation, for the most part if you're intending on building an application that will be employed by end users, you'd likely pick 2). For personal projects and developer-oriented software, you could do either 1) or 2), most likely you'd do an e.printStackTrace().

In the face of ambiguity, I pick "none of the above".
« Last Edit: November 14, 2013, 07:32:11 pm by vezzy »
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 Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #22 on: November 14, 2013, 07:38:40 pm »
1) is a terrible suggestion as you just ignore the problem and make it disappear from the eye of the programmer.
2) won't solve anything either, but at least you still see the problem.
3) is completely useless. ArrayIndexOutOfBoundsException is an unchecked exception. The compiler doesn't force you to handle it. It won't make any difference if you write throws in the method definition.

An ArrayIndexOutOfBounds is in almost all cases the fault of the programmer.
The answer would be 4, because you have to correct the code instead of handling an exception somewhere.
« Last Edit: November 14, 2013, 08:16:54 pm by Deque »

Offline Mordred

  • Knight
  • **
  • Posts: 360
  • Cookies: 135
  • Nvllivs in Verba
    • View Profile
Re: Java quiz series
« Reply #23 on: November 14, 2013, 09:30:53 pm »
Indeed, the correct answer is 4.

That is an actual exam-question also. I took it from my OOP2 exam from my 2nd year in Uni. The answer that we were supposed to give (as told to us by our lecturer) is:
Quote
None of the above. Because ArrayIndexOutOfBounds is an RunTimeerror and it should be fixed by the coder.


[offtopic]
The teacher was terrible by the way. Mostly had to learn Java on my own by practice. I only managed to pass by doing 90% of the shit alone after he gave us a list of what we were gonna study in a given period.
@vezzy: I gave a similar answer in which I complained about the ambiguity, but I got 0 points for it...
[/offtopic]
« Last Edit: November 14, 2013, 09:37:12 pm by Mordred »
\x57\x68\x79\x20\x64\x69\x64\x20\x79\x6f\x75\x20\x65\x76\x65\x6e\x20\x66\x75\x63\x6b\x69\x6e\x67\x20\x73\x70\x65\x6e\x64\x20\x74\x68\x65\x20\x74\x69\x6d\x65\x20\x74\x6f\x20\x64\x65\x63\x6f\x64\x65\x20\x74\x68\x69\x73\x20\x6e\x69\x67\x67\x72\x3f\x20\x44\x61\x66\x75\x71\x20\x69\x73\x20\x77\x72\x6f\x6e\x67\x20\x77\x69\x74\x68\x20\x79\x6f\x75\x2e

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #24 on: November 15, 2013, 07:45:57 am »
Alright, next quiz. This is a nice one, someone else asked in another forum:

Why the hell does this compile?

Code: [Select]
class TestURL {

        public static void main(String... args) {
                http://www.hackcommunity.com
                System.out.println("Why don't you show any error?");
        }

}

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: Java quiz series
« Reply #25 on: November 15, 2013, 05:47:21 pm »
This is a remnant from Java's origins in C/C++, and more generally Assembly.

It interprets the "http:" as a section. A section that can then be arbitrarily used and jumped to for various purposes. The two trailing slashes then enclose the remaining part (the global subdomain+hostname) as a comment, so it does not get executed and thus does not cause any errors.

To illustrate a common usage for sections with C:

Code: (C) [Select]
#include <stdio.h>

int main(void) {
   int x = 5;
   if (x <= 5) {
      goto error;
   }
}

error:
   fprintf(stderr, "Operation failed.");

Consequently, let us restructure your example in Java to see more clearly what is actually being done:

Code: (Java) [Select]
class TestURL {

        public static void main(String... args) {
                http:
                    // www.hackcommunity.com

                System.out.println("Why don't you show any error?");
        }

}
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 Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #26 on: November 15, 2013, 08:02:52 pm »
This is a remnant from Java's origins in C/C++, and more generally Assembly.

It interprets the "http:" as a section. A section that can then be arbitrarily used and jumped to for various purposes. The two trailing slashes then enclose the remaining part (the global subdomain+hostname) as a comment, so it does not get executed and thus does not cause any errors.

To illustrate a common usage for sections with C:

Code: (C) [Select]
#include <stdio.h>

int main(void) {
   int x = 5;
   if (x <= 5) {
      goto error;
   }
}

error:
   fprintf(stderr, "Operation failed.");

Consequently, let us restructure your example in Java to see more clearly what is actually being done:

Code: (Java) [Select]
class TestURL {

        public static void main(String... args) {
                http:
                    // www.hackcommunity.com

                System.out.println("Why don't you show any error?");
        }

}

It's called a label and can only be jumped to via break or condition statement.
You got it, you can make the next quiz.

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: Java quiz series
« Reply #27 on: November 15, 2013, 08:44:35 pm »
Code: (Java) [Select]
public class Example {
   public static void main(String[] args) {
      int x = 8;
      double y = 4.671268942105652;

      x = x + y;
      System.out.println(x);
     }
}

Will this compile? If so, why? If not, why and how would you fix it?
« Last Edit: November 15, 2013, 08:44:47 pm by vezzy »
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 #28 on: November 16, 2013, 09:13:39 am »
It will not compile, because you cannot convert from double to int without data loss.
How would I fix it? Depends, if i want a floating point result, chance type of x to double, if i want an int result add cast to int and deal with data loss.
<!--Here be Cthulhu -->

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Java quiz series
« Reply #29 on: November 16, 2013, 01:38:05 pm »
Looking from a science standpoint,  you would go by significant figures, which would be 8 in this case. So I would cast int.
>>>import this
-----------------------------