Java Multiple Choice Questions & Answers on Character Extraction for Freshers

1. Which of these method of class String is used to extract more than one character at a time a String object?

a) getchars()
b) GetChars()
c) Getchars()
d) getChars()
Answer: d
2. Which of these methods is an alternative to getChars() that stores the characters in an array of bytes?

a) getBytes()
b) GetByte()
c) giveByte()
d) Give Bytes()
Answer: a

Explanation: getBytes() stores the character in an array of bytes. It uses default character to byte conversions provided by platform.
3. In below code, what can directly access and change the value of the variable name?
  1.  package test;
  2.  class Target 
  3.  {
  4.   public String name = "hello";
  5.  }
a) any class
b) only the Target class
c) any class in the test package
d) any class that extends Target
Answer: c

Explanation: Any class in the test package can access and change name.
4. What will be output of the following code?
  1. public class Boxer1 {
  2.     Integer i;
  3.     int x;
  4.  public Boxer1(int y) {
  5.     x = i+y;
  6.     System.out.println(x);
  7.  }
  8.  public static void main(String[] args) {
  9.     new Boxer1 (new Integer(4));
  10.  }
  11. }
a) The value “4” is printed at the command line
b) Compilation fails because of an error in line
c) A NullPointerException occurs at runtime
d) An IllegalStateException occurs at runtime
Answer: d

Explanation: Because we are performing operation on reference variable which is null.
5. Which of these methods can be used to convert all characters in a String into a character array?

a) charAt()
b) both getChars() & charAt()
c) both toCharArray() & getChars()
d) All of the mentioned
Answer: c

Explanation: charAt() return one character only not array of character.
6. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         { 
  4.            String c = "Hello i love java";
  5.            int start = 2;
  6.            int end = 9;
  7.            char s[]=new char[end-start];
  8.            c.getChars(start,end,s,0);
  9.            System.out.println(s);
  10.         }
  11.     }
a) Hello, i love java
b) i love ja
c) lo i lo
d) llo i l
Answer:d

Explanation: getChars(start,end,s,0) returns an array from the string c, starting index of array is pointed by start and ending index is pointed by end. s is the target character array where the new string of letters is going to be stored and the new string will be stored from 0th position in s.
7. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         {
  4.             String a = "hello i love java";
  5.             System.out.println(a.indexOf('i')+" "+a.indexOf('o') +" "+a.lastIndexOf('i')+" "+a.lastIndexOf('o'));
  6.         }
  7.     }
a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 4 3 6 9
Answer:a

Explanation: indexof(‘c’) and lastIndexof(‘c’) are pre defined function which are used to get the index of first and last occurrence of
the character pointed by c in the given array.
8. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         {
  4.             char c[]={'a', '1', 'b' ,' ' ,'A' , '0'};
  5.             for (int i = 0; i < 5; ++i)
  6.             {
  7.                    if(Character.isDigit(c[i]))
  8.                        System.out.println(c[i]+" is a digit");
  9.                    if(Character.isWhitespace(c[i]))
  10.                        System.out.println(c[i]+" is a Whitespace character");
  11.                    if(Character.isUpperCase(c[i]))
  12.                        System.out.println(c[i]+" is an Upper case Letter");
  13.                    if(Character.isLowerCase(c[i]))
  14.                        System.out.println(c[i]+" is a lower case Letter");
  15.                i=i+3;
  16.             }
  17.         }
  18.     }
a) a is a lower case Letter
is White space character
b) b is a lower case Letter
is White space character
c) a is a lower case Letter
A is a upper case Letter
d) a is a lower case Letter
0 is a digit
Answer:c

Explanation:Character.isDigit(c[i]),Character.isUpperCase(c[i]),Character.isWhitespace(c[i]) are the function of library java.lang. They are used to find weather the given character is of specified type or not. They return true or false i:e Boolean variable.
9. What is the output of this program?
  1.     class String_demo {
  2.         public static void main(String args[])
  3.         {
  4.             char chars[] = {'a', 'b', 'c'};
  5.             String s = new String(chars);
  6.             System.out.println(s);
  7.         }
  8.    }
a) a
b) b
c) c
d) abc
Answer: d

Explanation: String(chars) is a constructor of class string, it initializes string s with the values stored in character array chars, therefore s contains “abc”.
10. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         {
  4.             char ch;
  5.             ch = "hello".charAt(1);
  6.             System.out.println(ch);
  7.         }
  8.    }
a) h
b) e
c) l
d) o
Answer: b

Explanation: “hello” is a String literal, method charAt() returns the character specified at the index position. Character at index position 1 is e of hello, hence ch contains e.

Related

Computer Fundamentals Multiple choice Questions and Answers on IEEE 32 and 64 bit for Freshers

1. Binary addition of 1 + 1 gives the result _____________ a) 0b) 1c) 2d) 10 Answer: a Explanation: The result obtained is 0 with a carry of 1. This carry obtained is added to the next higher col...

Computer Fundamentals Multiple choice Questions and Answers on Complements for Freshers

1. What is the 1’s complement of 11010? a) 11010b) 11011c) 00110d) 00101 Answer: d Explanation: The 1’s complement of a number is obtained by converting all the 0 bits to 1 and all 1’s to 0’s. He...

Multiple Choice Questions and Answers on Cloud Commons and SMI of Cloud Computing for Freshers

1. Which of the following is a core management feature offered by most cloud management service products ? a) Support of different cloud typesb) Creation and provisioning of different types of clou...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item