I dont really know specifically what im searching for, which is why i haven't just found this via searching the web. Have you guys come accross or know of any sort of quiz questions like where it gives you a for loop which isn't in the normal format and then asks you what would x be? or how many times will this println?

Just wanna get some exam practise. Thank you.

Dani AI

Generated

Classic "trick" quiz items like the example posted by are testing Java's expression-evaluation rules and side effects (prefix vs postfix ++/--), not arithmetic intuition. These problems are best solved by applying the language rules deterministically rather than guessing. The authoritative reference is the Java Language Specification; use it to confirm how operands are evaluated and when side effects take effect.

A reliable way to answer these quickly on an exam or in an interview:

  • Rewrite the complex expression as a sequence of simple steps using temporary names.
  • Evaluate subexpressions strictly left-to-right, applying the documented semantics: a prefix increment yields the incremented value; a postfix increment yields the original value and produces a side effect.
  • Annotate each step with the variable state before and after the side effect.
  • If still unsure, compile and step through with a debugger or write a tiny unit test to observe exact behavior. This is faster than trying to mentally juggle multiple side effects.

Useful focused practice and references:

A final note: these puzzles are great for sharpening understanding, but writing code that relies on stacked side effects is error-prone. In real projects prefer clarity over cleverness and document any non-obvious intent.

Recommended Answers

All 3 Replies

I personally found use of pre/post ++/-- operators combined with self addition / substraction quite frustrating.
E.g.

int i = 0;
		for(; i < 10; i++) {
		     i -= i++ - --i;
		     //OR try this: i += ++i + i--;
		     System.out.println("Hello World!") ;
		}

Q = how many times would you hello the world?

Haha yeah thats what i meant mate, couldnt really describe it, do you have any links to these kind of questions en masse? lol that question you posted is bloody evil. Without compiling it I'm guessin it would print hello world 10 times beccause the sum is a double negative and so i iterates as ++1 each time? that right m8?

10

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.