42
\$\begingroup\$

The goal is to write program that will produce a word unexpectedly (program doesn't look like it is going to produce a word).

Example (JavaScript):

alert(([0][1]+"").slice(4,8))

alerts "fine" - jsfiddle.

Explanations:

1. [0][1] returns undefined, because there only one element in array [0]
2. Adding empty string converts undefined to string "undefined"
3. slice(4,8) outputs fifth to eight characters of undefined


This is underhanded popularity contest.

\$\endgroup\$

closed as off-topic by Alex A. Apr 15 '16 at 3:59

  • This question does not appear to be about programming puzzles or code golf within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ I don't think this challenge is objectively measurable. How "unexpected" is "unexpected enough"? \$\endgroup\$ – Martin Ender May 31 '14 at 16:28
  • 1
    \$\begingroup\$ I think this is more or less a dupe of the "Print an unexpected obscenity" question which has now been deleted so I can't find the link. That one was also an underhanded about unexpectedly printing a word, with the constraint that it had to be an insult \$\endgroup\$ – user16402 May 31 '14 at 17:32
  • 1
    \$\begingroup\$ I'm closing this question because, by community consensus, underhanded challenges are no longer welcome on the site. \$\endgroup\$ – Alex A. Apr 15 '16 at 3:59

47 Answers 47

3
\$\begingroup\$

Java

This code is a bit long, outputs word:

SEVERE

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Text
{
    public static void main( String args[] )
    {
        makeSureWeCantOutputText();
        unexpected();
    }

    private static void unexpected()
    {
        try
        {
            //this does nothing
            long x = 1;
            for( int i = 1; i / i == 1; i++ )
            {
                x++;
            }
        }
        catch( Exception ex )
        {
            Logger.getLogger( Text.class.getName()).log( Level.SEVERE, "");
        }
    }

    private static void makeSureWeCantOutputText()
    {
        try
        {
            //OutputStream class that outputs to nowhere
            PrintStream stream = new PrintStream( new OutputStream()
            {

                @Override
                public void write( int b ) throws IOException
                {
                }

            } );

            //replace System.out field with OutputStream that does nothing
            Field field = System.class.getDeclaredField( "out" );
            field.setAccessible( true );

            Field modifiersField = Field.class.getDeclaredField( "modifiers" );
            modifiersField.setAccessible( true );
            modifiersField.setInt( field, field.getModifiers() & ~Modifier.FINAL );

            field.set( null, stream );
        }
        catch( Exception e )
        {
            //we don't want to outpout anything if it fails
        }
    }
}

Explanation:

When exception is thrown we use Logger. Logger with Level.SEVERE Logger uses System.err instead of System.out. i / i throws exception when i reaches 0 (it starts from 1 and increases, then integer overflows to Integer.MIN_VALUE and is increased to 0).

\$\endgroup\$
3
\$\begingroup\$

Ruby

#!/usr/bin/ruby
a = self.methods.map{|x|x.to_s}
b = a.count
p a[b/2-8]+a[b-6]

Outputs:

"freeze!"
\$\endgroup\$
3
\$\begingroup\$

Ruby

Bit late to the party, but couldn't help but notice that no-one had done something like this:

puts 65791925216734442002988169043447582627179158156970580.to_s(36)

Demo

\$\endgroup\$
2
\$\begingroup\$

JavaScript

function rgbToHex(rgb) {
  var r = rgb[0].toString(16);
  var g = rgb[1].toString(16);
  var b = rgb[2].toString(16);
  return '#' + r + g + b;
}

alert(rgbToHex([176,0,11]));
\$\endgroup\$
  • 1
    \$\begingroup\$ The hex converter doesn't properly zero pad and yes it alerts #b00b. \$\endgroup\$ – wolfhammer Jun 13 '14 at 0:56
2
\$\begingroup\$

JavaScript

Test it on your browser's console

x = Math.random() > 0.5 ? 2 : 0;
alert(document.getElementsByClassName('question-hyperlink')[0].text.substr(13 - x,13 + x))
\$\endgroup\$
  • \$\begingroup\$ Yep, good. Wonderful. No need to add explanation )) However if word is expected, I expect it. Anyway, +1. \$\endgroup\$ – nicael Jun 18 '14 at 11:53
2
\$\begingroup\$

J

This is what happens if you're bored.

'Nqbs a `u{c vac'({&a.@-@(t@[-+:@t=:a.&i.@]))'Kill a good man'

Output:

Have a nice day.

Why?

It calculates -y - x - x where x is the left string and yis the right string.

\$\endgroup\$
  • \$\begingroup\$ And it for sure isn't taking home the cleverness award, but I like it. \$\endgroup\$ – seequ Jun 19 '14 at 19:21
2
\$\begingroup\$

IBM Enterprise COBOL

   ID DIVISION. 
   PROGRAM-ID. ERRMSG. 
   ENVIRONMENT DIVISION. 
   DATA DIVISION. 
   WORKING-STORAGE SECTION.
   01  A PIC 9 VALUE ZERO. 
   PROCEDURE DIVISION. 
       ADD 1 TO A 
       GOBACK 
       . 

Here's a short program. It obviously does nothing but add one to the field A.

However, toss it into the compiler, and... http://www-01.ibm.com/support/docview.wss?uid=swg21156178

You can generate a complete listing of compiler diagnostic messages with their >message numbers, severities, and text by compiling a program that has program-name ERRMSG.

You can code just the PROGRAM-ID paragraph, as shown below, and omit the rest >of the program.

The key is to have errmsg as the PROGRAM-ID. No other code is required. The code in my example is just to convince that it is a real program which produces no output.

This is the example IBM show in the link:

Identification Division.
Program-ID. ErrMsg.

Indeed, this is enough:

Program-ID. ErrMsg.

Or this:

Program-ID. ErrMsg.
The quick brown fox jumped over the 
lazy dog, or any other text you like.

This is an example of the output: http://www-01.ibm.com/support/docview.wss?uid=swg21156178&aid=1

When the compiler gets to errmsg. (note the full-stop/period, which doesn't have to be immediately after errmsg, but must exist after it somewhere in the source) it knows what is needed, reads to the end of the source and then produces the listing, including the description of the format and information contained in the error messages, and all the messages themselves.

As a curiosity, you can try this:

Program-ID. ErrMsg

This time no full-stop/period is encountered before the end of the source is reached, and a (very) minor compiler bug is revealed, as the compiler continues reading, and abends (ends abnormally, crashes) with an S001 Reason Code 5 and

IEC020I GET ISSUED AFTER END-OF-FILE

No IBM Mainframe COBOL has ever separately documented its error messages in an actual manual.

\$\endgroup\$
1
\$\begingroup\$

Javascript - 19 chars

This one is inspired by Gary Bernadt's 'wat' talk: Video Link

([]+{}).substr(1,6)

Unexpected because Array [] + Object {} should not output an Object.

"object"

Another solution suggested by bebe

(0+{}).slice(2,8)

"object"

\$\endgroup\$
1
\$\begingroup\$

Perl

open IN, "<".$0;
while (<IN>) {
        chomp;
        (m/(^|[^\)])w.i.e/ && (s/^.(.).(.)(.).*/$1$3$2$2/ && print $_)) || m/(^|[^\)])...se/ && (s/^..(.).*/$1/ && print $_."\n");
}
close IN;

Opens it's own script file and prints out some of the characters.

\$\endgroup\$
1
\$\begingroup\$

C++

#include <iostream>
#include <limits.h>
#include <math.h>
using namespace std;
typedef unsigned long long ULL;
#define C(x) cout<<(ULL)(x)<<endl
int main ()
{
    ULL b=UINT_MAX;
    ULL d=LLONG_MAX;

    ULL f=(ULL)(d/b);
    size_t n = (log(f)/log(10))+1;
    char *w=new char[n];
    char x;
    for(size_t i=0; i<n; i++)
    {
        x = (char)((f%10)+'@');
        f=(ULL)(f/10);
        w[n-i-1]=((n-i)<4)?x:0;
    }
    cout<<w<<endl;
    return 0;
}

Produces the word "BAD".

\$\endgroup\$
1
\$\begingroup\$

Mathematica

SeedRandom[377]; FromCharacterCode@RandomInteger[{98, 123}, 5]

outputs proof

BaseForm[272580165883129, 29]

outputs impossible

You can try the second one online in Mathics. Type in the code and press Shift-Enter. The first one is implementation specific, thus it won't work in Mathics, but it does work in all recent versions of Mathematica.

\$\endgroup\$
  • \$\begingroup\$ outputs "impossible[base 29]", the 29 is beneath impossible. Why is it impossible? \$\endgroup\$ – Mega Man Aug 8 '16 at 19:15
  • \$\begingroup\$ @MegaMan I don't understand your question \$\endgroup\$ – Szabolcs Aug 8 '16 at 20:14
1
\$\begingroup\$

Extended BrainFuck

>~~ufp~<.[-.<]

outputs "poet"

\$\endgroup\$
  • 1
    \$\begingroup\$ Everything what bf and its derivates do is unexcepted... \$\endgroup\$ – Mega Man Aug 8 '16 at 19:23
1
\$\begingroup\$

Burlesque

blsq ) 4593109656 128B!
"hello"
blsq ) vvSh29.-4.+
"size"

Explanation

128B! is just base 128.

vvSh29.-4.+ produces an error, converts the error to a string and then slices.

\$\endgroup\$
1
\$\begingroup\$

Mouse-2002

' '  + 20 + b + a + !'
' '  + 13 + i + z + !'
' '  + 4  + i + z + !'
' '  + 23 + i + z + !'
' '  + 15 + i + z + !'
' '  + 4  + i + z + !'
' '  + 2  + i + z + !'
' '  + 19 + i + z + !'
' '  + 4  + i + z + !'
' '  + 3  + i + z + !'
'  '   +  s  -  !'
'  '   +  n n + n n + + c + -  !'

$

Output:

Unexpected.

Explanation:

uses ascii charcodes. the letters refer to addresses, which are integers in the range 0-25.

\$\endgroup\$
0
\$\begingroup\$

PHP, 59 or 31 chars

Does it have to be a real word? If not, this will work:

echo preg_replace('/[0-9]+/',"",substr(md5(time()),0,25));

or even this if numbers are okay:

echo substr(md5(time()),0,25); 

Set a range

echo substr(md5(time()),0,rand(1,50)); 
\$\endgroup\$
  • \$\begingroup\$ I am so stupid. I mean "knife", not "nife". As I can not produce "knife", I edited my example to produce "fine". (Such word exists, isn't it? :) ) \$\endgroup\$ – nicael May 31 '14 at 17:29
  • \$\begingroup\$ I clarified my question. \$\endgroup\$ – nicael Jun 2 '14 at 19:30
0
\$\begingroup\$

Haskell

Classic:

let 1+1="Hello World!" in 1 + 1

Outputs Hello World!. No kidding.

\$\endgroup\$
  • \$\begingroup\$ Should be a word. \$\endgroup\$ – CalculatorFeline Jun 20 '17 at 22:30
0
\$\begingroup\$

Mathematica

ToString[#&@;;]~StringTake~{16,20}

Prints "Dummy". But I seriously do not understand why #&@;; does..what it does.

\$\endgroup\$
  • \$\begingroup\$ Funny note: None of the letters Dumy appear in the code. \$\endgroup\$ – CalculatorFeline Nov 13 '16 at 23:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.