Topics Index

Strnigs and Return statement

If you need explanation Read this topic

If you need Answer Take test on this topic

Strnigs and Return statement
What will be the output of the following program?
public class Classifier {
    public static void main(String[] args) {
        System.out.print(type('Z') + type('\'') + type('6'));
    public static String type(char c) {
        if ("0123456789".indexOf(c) >= 0)
            return "Number ";
        if ("abcdefghijklmnopqrstuvwxyz".indexOf(c) >= 0)
            return "Letter ";
        if ("\"'+-*/&|!=".indexOf(c) >= 0)
            return "Special-Character ";
        return "Incorrect ";

A. Letter Special-Character Number
B. Letter Incorrect Number
C. Incorrect Special-Character Number
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java Searching Strings - Java indexOf, lastIndexOf Methods

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans c
Z type returns incrct
\' retuns spcl char
6 type numbr

Posted by Maheshwari Natarajan    2014-07-29 12:45:00

answer d. som othr
Z returns incorrct
\' incorrct
6 number

Posted by NagaRaj Nataraj    2014-07-29 12:47:33

Answer is C
Output is:- IncorrectSpecial-CharacterNumber
for the letter Z in type method it returns Incorrect because the argument passed is Captial 'Z' but accordind to the condition z should be small 'z'
for the SpecialCharacter \ it returns Special-character
for the number 6 it returns Number
and all the 3 will get concatenated by + operator as they are strings the output will be as above.

Posted by Sai Avinash    2014-07-29 12:56:38

answer is c.

Posted by Shubham Bansal    2014-07-29 12:57:26

only catch here is in type('\'')
\' is an escape sequence so it will match with special character '

Posted by Tushar Ghude    2014-07-29 13:35:17

Congratulations Maheshwari Natarajan. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-07-30 04:20:18

© meritcampus 2019

All Rights Reserved.

Open In App