Character literals questions on java and answer with solution

Objective types questions and answers of character literals in java



(1)

public class Literal {
    public static void main(String[] args) {
         char b ='\n';
         int a=(int)b;
         System.out.println(a);
    }
}

What will output when you compile and run the above code?

(a)10
(b)11
(c)12
(d) Compiler error




Answer: (a)


(2)

public class Literal {
    public static void main(String[] args) {
         char a ='A';
         char b='0';
         char c=(char)(a+b-16);
         System.out.println(c);
    }
}

What will output when you compile and run the above code?

(a)97
(b)a
(c)’a’
(d) Compiler error





Answer: (b)

(3)

public class Literal {
    public static void main(String[] args) {
         char a ='\';
         System.out.println("C:"+a+"TC"+a+BIN);
    }
}

What will output when you compile and run the above code?

(a)C:\TC\BIN
(b)C:TCBIN
(c)C:    C\BIN
(d) Compiler error




Answer: (d)

(4)

public class Literal {
    public static void main(String[] args) {
         char a ='\15';
         int b=3*a;
         System.out.println(b);
    }
}

What will output when you compile and run the above code?

(a)45
(b)-3
(c)39
(d)Compiler error




Answer: (c)

(5)

public class Literal {
    public static void main(String[] args) {
         char a ='\90';
         int b=a+7;
         System.out.println(b);
    }
}

What will output when you compile and run the above code?

(a)97
(b)a
(c)35
(d) Compiler error





Answer: (d)

Java questions of data types and answer
Java questions on if else statements
Java questions on switch case and answers
Java questions on looping and answers
Java questions on characters
Java questions on strings and answers 
Java questions on variables and answers
Java questions on automatic type promotions
Java questions on bit wise operator
Java questions on operators and answers
Java questions on static keyword and answers
Java questions on super keyword and answers
Java questions on abstract class an answers
Java questions on interface and answers
Java questions on enum data type and answers
Java questions on break and continue keyword
Java questions of primitive data types
Java questions on conditional operators
Java questions on two dimensional array and answers

No comments: