Author Topic: Java quiz series  (Read 23561 times)

0 Members and 3 Guests are viewing this topic.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Java quiz series
« on: November 13, 2013, 08:54:36 am »
For this challenge series I will ask some Java questions. The first one to answer the question correctly is the winner. Once the question is answered the winner is free to post the next quiz him-/herself, or -- if he/she has no idea for a challenge I will post the next quiz.



First quiz:

Code: [Select]
int i = (byte) + (char) - (int) + (long) - 1;
System.out.println(i);

Does this compile?
Explain what is happening and why.



Quizlers:

Neea: 1
Snayler: 1.5
ca0s: 1
Mordred: 1/2
« Last Edit: November 14, 2013, 07:16:54 pm by Deque »

Offline Neea

  • Peasant
  • *
  • Posts: 83
  • Cookies: 19
  • Laboris Gloria Ludi
    • View Profile
Re: Java quiz series
« Reply #1 on: November 13, 2013, 10:30:14 am »
Love the idea for the quiz :) Makes me want to learn java better.


Yes it compiles and the result is 1
You're basically mixing casts with operators ....
So you first have -1, then you cast it to long so it's gonna be long -1
Then you have + unary operation so it remains long -1, then you cast it to int => int -1
Then you have - operation => int 1, after cast to char => char 1
After + operation so it's gonna be char 1
Then cast to byte => byte 1
And in the end implicitly cast to int => int 1


Think that's about right.
I have no idea for the next challenge cuzz i don't know java that well :)
<!--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 #2 on: November 13, 2013, 10:37:27 am »
Love the idea for the quiz :) Makes me want to learn java better.


Yes it compiles and the result is 1
You're basically mixing casts with operators ....
So you first have -1, then you cast it to long so it's gonna be long -1
Then you have + unary operation so it remains long -1, then you cast it to int => int -1
Then you have - operation => int 1, after cast to char => char 1
After + operation so it's gonna be char 1
Then cast to byte => byte 1
And in the end implicitly cast to int => int 1


Think that's about right.
I have no idea for the next challenge cuzz i don't know java that well :)

Are you sshackss?
If so I would like you to answer only in one forum and leave the fun for others.

Your answer is right. Next quiz:

Code: (Java) [Select]
public class Foo {
    public static System sys = null;
 
    public static void main(String[] args) {
        sys.out.println("Foo");
    }
}

Does that work?
Why or why not?
« Last Edit: November 13, 2013, 12:36:36 pm by Deque »

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Java quiz series
« Reply #3 on: November 13, 2013, 06:07:25 pm »
"sys" object is null, such an exception can cause a black hole to appear. Avoid null objects at all costs!
BAM!

My question:

Code: [Select]
public class Foo {
public static void main(String[] args) {
System.out.println("Line1"+
                           "Line2"+
"Line3"+
"Java is the shit!!");
}
}

Does it print each line in a new line.

Also fuck CODE formatting...

Offline Snayler

  • Baron
  • ****
  • Posts: 812
  • Cookies: 135
    • View Profile
Re: Java quiz series
« Reply #4 on: November 13, 2013, 06:51:57 pm »
I'll start by saying I've never touched on Java.

No. It will print out:
Code: [Select]
Line1Line2Line3Java is the shit!!
"\n" is the shit!!

Oh, and I have no idea about the next challenge..
« Last Edit: November 13, 2013, 06:52:38 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 #5 on: November 13, 2013, 08:03:08 pm »
"sys" object is null, such an exception can cause a black hole to appear. Avoid null objects at all costs!
BAM!

Your answer is wrong. You get -1 for not waiting for confirmation.

My quiz is still up.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Java quiz series
« Reply #6 on: November 13, 2013, 09:03:22 pm »
Ok, my bad. I can't understand why that code does what it does, I mean, there's a null in there, for christ sake!

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: Java quiz series
« Reply #7 on: November 13, 2013, 11:46:46 pm »
out is a static member of the System class, so that code doesn't really use the sys object, but the class instead?

Offline Mordred

  • Knight
  • **
  • Posts: 360
  • Cookies: 135
  • Nvllivs in Verba
    • View Profile
Re: Java quiz series
« Reply #8 on: November 14, 2013, 01:15:25 am »
My answer:

It will work, as in it will display "Foo" on the screen (no compilation error).
Although the definition's correct as far as syntax goes, the Foo class tries to instantiate a variable called sys as a System object (albeit as null).
Unfortunately, although System, as all other classes in Java, extends Object, it is defined in the API as:
Code: [Select]
public final class System extends Object
So it is a final class, which means it cannot be instantiated at all. All calls to the System class are made directly, through a syntax such as:
Code: [Select]
System.out.println("Foo");
System.out.printf("Foo%n");

That null tho has me a bit worried, but I think this should be the correct answer  :) .
« Last Edit: November 14, 2013, 01:28:25 am 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 Traitor4000

  • Knight
  • **
  • Posts: 191
  • Cookies: 8
    • View Profile
Re: Java quiz series
« Reply #9 on: November 14, 2013, 04:11:37 am »
 :o  I am surrounded by people way smarter than me...
The most vulnerable part of an impenetrable system is those who believe it to be so.

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: Java quiz series
« Reply #10 on: November 14, 2013, 01:00:12 pm »
My answer:

It will work, as in it will display "Foo" on the screen (no compilation error).
Although the definition's correct as far as syntax goes, the Foo class tries to instantiate a variable called sys as a System object (albeit as null).
Unfortunately, although System, as all other classes in Java, extends Object, it is defined in the API as:
Code: [Select]
public final class System extends Object
So it is a final class, which means it cannot be instantiated at all. All calls to the System class are made directly, through a syntax such as:
Code: [Select]
System.out.println("Foo");
System.out.printf("Foo%n");

That null tho has me a bit worried, but I think this should be the correct answer  :) .
Final means that it cannot be subclassed. A final class can be instantiated.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #11 on: November 14, 2013, 02:32:44 pm »
out is a static member of the System class, so that code doesn't really use the sys object, but the class instead?

You nailed it.

Do you have another quiz?

Foo class tries to instantiate a variable called sys as a System object (albeit as null).

sys is not instantiated, there is no object created from the System class in that code.  ;)
And, yes, as ca0s said correctly it has nothing to do with System being final.
System can indeed not be instantiated, but the reason for that is the private constructor.

Let's look at the code of System:

 
Code: [Select]
/** Don't let anyone instantiate this class */
    private System() {
    }

    public final static PrintStream out = null;

As you can see out is a static member of the class, which is the reason you access it usually via System.out

Offline Mordred

  • Knight
  • **
  • Posts: 360
  • Cookies: 135
  • Nvllivs in Verba
    • View Profile
Re: Java quiz series
« Reply #12 on: November 14, 2013, 02:49:12 pm »
Oh true. Welp, I guess they should've taught us this in one of the 5 Java courses I took during University ^^ (or should I say: "they probably taught us but I didn't give enough of a fuck"?  ::) )
« Last Edit: November 14, 2013, 02:50:17 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 ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: Java quiz series
« Reply #13 on: November 14, 2013, 04:15:49 pm »
Quote
Do you have another quiz?
Sorry, I don't. I'm not that fluent in Java to be able to propose an interesting quiz.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java quiz series
« Reply #14 on: November 14, 2013, 04:49:09 pm »
Alright. The next one is an (hopefully) easy question.
As you know Java has the access modifiers: private, public and protected.
What is the default access level if you don't use an access modifier?

Example:

Code: (Java) [Select]
public class AccessTest {

    void methodWithoutModifier() { }

}
« Last Edit: November 14, 2013, 04:49:27 pm by Deque »