# Produce an unexpected word [closed]

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))


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.

• I don't think this challenge is objectively measurable. How "unexpected" is "unexpected enough"? – Martin Ender May 31 '14 at 16:28
• 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 – user16402 May 31 '14 at 17:32
• I'm closing this question because, by community consensus, underhanded challenges are no longer welcome on the site. – Alex A. Apr 15 '16 at 3:59

# 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).

# 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!"


# 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

# 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;
}


• The hex converter doesn't properly zero pad and yes it alerts #b00b. – wolfhammer Jun 13 '14 at 0:56

## JavaScript

Test it on your browser's console

x = Math.random() > 0.5 ? 2 : 0;

• Yep, good. Wonderful. No need to add explanation )) However if word is expected, I expect it. Anyway, +1. – nicael Jun 18 '14 at 11:53

# 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.

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

IBM Enterprise COBOL

   ID DIVISION.
PROGRAM-ID. ERRMSG.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  A PIC 9 VALUE ZERO.
PROCEDURE DIVISION.
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.

## Javascript - 19 chars

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


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

"object"

Another solution suggested by bebe

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


"object"

# 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.

## 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;
}


## 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.

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

# Extended BrainFuck

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


outputs "poet"

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

## 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.

# 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.

# 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));

• 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? :) ) – nicael May 31 '14 at 17:29
• I clarified my question. – nicael Jun 2 '14 at 19:30

Classic:

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


Outputs Hello World!. No kidding.

• Should be a word. – CalculatorFeline Jun 20 '17 at 22:30

# Mathematica

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


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

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