• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

Recent Posts

Python Puzzle 4

Python Puzzle 4

What is the output of def foo(bar=None, **kwargs): print("\tbar={}".format(bar)) print("\tkwargs={}".format(kwargs)) print("Test 1:") foo(bar=12, holla=13) print("Test 2:") foo(holla=13, bar=12) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Output: Test 1: bar=12 kwargs={'holla': 13} Test 2: bar=12 kwargs={'holla' … Read More »
C Puzzle #3

C Puzzle #3

What is the output of the following programm? #include int main() { printf("%i\n", -13>>1); } . . . . . . . . . . . . . . . . . . . Short answer -7 Long answer Signed integers are two’s complement binary values that can be used to represent both positive and negative integer values. Source: Intels IA-32 Architectures Software Developer's Manuals … Read More »
Java Puzzle #14: Integers

Java Puzzle #14: Integers

What is the output of the following script? public class SomeClass { public static void main(String[] args) { int x = 2147483647; // 2147483647 == 2**31 - 1 if (x < 2*x) { System.out.println("Everything's ok:"); } else { System.out.println("It's weird:"); } System.out.println("x = " + x); System.out.println("2*x = " + 2 … Read More »
C Puzzle #2

C Puzzle #2

What is the output of the following script? Why? How can it be fixed to get the expected output? #define __STDC_FORMAT_MACROS #include #include int main() { uint64_t testvar; testvar = 1<<30; printf("2^30 = %" PRIu64 "\n", testvar); testvar = 1<<31; printf("2^31 = %" PRIu64 "\n", testvar); return … Read More »
Java Puzzle #13: Absolute Value Weirdness

Java Puzzle #13: Absolute Value Weirdness

What does the following Java snippet output? public class SomeClass { public static void main(String[] args) { int a = -10; int b = -2147483648; // -2147483648 == -2**31 if (Math.abs(a) < -1) { System.out.println("|a| < -1"); } else { System.out.println("|a| >= -1"); } if (Math.abs(b) < -1) { System.out.println("|b … Read More »
Java Puzzle #12: Control-flow

Java Puzzle #12: Control-flow

What is the output of the following HelloWorld.java? public class HelloWorld { public static void main(String[] args) { if (2 < 1); { System.out.println("Yes"); } } } . . . . . . . . . . . Answer The output is "Yes". Explanation The ; means, that no block is executed. The { ... } is just a code block and not really related to the … Read More »
Java Puzzle #11: Change argument of foreach

Java Puzzle #11: Change argument of foreach

What is the output of the following HelloWorld.java? import java.util.LinkedList; public class HelloWorld { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add(1); list.add(2); list.add(3); int i = 0; for (Integer el : list) { System.out.println(el); list.add … Read More »
Python Puzzle #3: Associativity

Python Puzzle #3: Associativity

What is the output of 1 in [] in "a" and what is the output of (1 in []) in "a" or 1 in ([] in "a") . . . . . . . . . . . . . . . . . . . . . . . . . Answer The first expression evaluates to False, because it gets evaluated as (1 in []) and ([] in 'a') (Manual, Source). The second two expressions are invalid; they … Read More »
Java Puzzle #10: Multiple Interfaces

Java Puzzle #10: Multiple Interfaces

You have to following source code: A.java: public interface A { public int methodA(double a, int b, char c); public int methodB(); } B.java: public interface B { public int methodB(); public void methodC(); } test.java: public class test implements A, B { public static void main(String[] args) { test t … Read More »
Java Puzzle #9: Template method pattern

Java Puzzle #9: Template method pattern

The following Java Puzzle is an example for the template method pattern. It is a design pattern by the Gang of Four. What is the output of the following snippet: AbstractClass.java: public class AbstractClass { int templateMethod() { return simpleOperation1() * simpleOperation2(); } int simpleOperation1() { return 2; } int simpleOperation2() { return 3; } } ConcreteClass.java … Read More »
  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor