More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. FALSE. == operator compares object references, a and b are references to two different objects, hence the FALSE. .equals method is used to compare string object content.
Help us improve. Please let us know the company, where you were asked this question :
Ans. public static void main(String ar[])
{
int n=5;
if((n/2)*2==n)
{
System.out.println("Even Number ");
}
else
{
System.out.println("Odd Number ");
}
}
Help us improve. Please let us know the company, where you were asked this question :
Q4. How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
Ans. Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Compile time error as it won't find the constructor matching BuggyBread2().
Compiler won't provide default no argument constructor as programmer has already defined one constructor.
Compiler will treat user defined BuggyBread2() as a method, as return type ( void ) has been specified for that.
Help us improve. Please let us know the company, where you were asked this question :
In what order the elements of a HashSet are retrieved ?
Random Order
Insertion Order
Natural Sorting Order
Inverse Natural Sorting Order
Q13. What will be the output of the following code ?
enum Day {
MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY
}
public class BuggyBread1{
public static void main (String args[]) {
Set mySet = new TreeSet();
mySet.add(Day.SATURDAY);
mySet.add(Day.WEDNESDAY);
mySet.add(Day.FRIDAY);
mySet.add(Day.WEDNESDAY);
for(Day d: mySet){
System.out.println(d);
}
}
}
Only one FRIDAY will be printed as Set doesn't allow duplicates.Elements will be printed in the order in which constants are declared in the Enum. TreeSet maintains the elements in the ascending order which is identified by the defined compareTo method. compareTo method in Enum has been defined such that the constant declared later are greater than the constants declared prior.
Help us improve. Please let us know the company, where you were asked this question :
Q15. Write a program to print all even numbers first and then all odd numbers till 100. For example the output should be
2
4
6
8
...... 100
and then
1
3
5
7
...... 99
using only 1 for loop ? Is it possible with just one loop ?
public class BuggyBread{
public static void main (String args[]) {
int x = 50;
for(int i=1;i <= 100;i++){
if(i<=50){
System.out.println(i*2);
} else {
System.out.println(i-x);
x = x - 1;
}
}
}
}
Help us improve. Please let us know the company, where you were asked this question :
Ans. import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.*;
public class Common {
public static void main(String ar[])throws Exception
{
File f=new File("a.txt");
File f1=new File("x.java");
System.out.println(f.exists());
FileInputStream fin = new FileInputStream(f);
FileInputStream fin1 = new FileInputStream(f1);
Ans. int duplicateArray[] = { 1, 2, 2, 3, 4, 5, 6, 8, 9}
Set unique = new HashSet();
for (int i = 0; i < duplicateArray.length; i) {
if (unique.contains(duplicateArray[i])) {
System.out.println(duplicateArray[i]);
} else {
unique.add(duplicateArray[i]);
}
}
Complexity O(n) = nHashSet contains and add has O(n) = 1
Help us improve. Please let us know the company, where you were asked this question :
Ans. A way of encoding characters as binary numbers. The Unicode character set includes characters used in many languages, not just English. Unicode is the character set that is used internally by Java.
Help us improve. Please let us know the company, where you were asked this question :
public static void main(String[] args) throws InterruptedException{
int totalSum = 0;
// Create all the threads and set the count starting point for all of them
for(int count=0;count<10;count++){
TotalUsingThreads newThread = new TotalUsingThreads(count*100);
newThread.start();
collector.add(newThread);
}
boolean allCollected = false;
// Make sure that all thread totals are collected and all threads have completed their tasks
while(!allCollected){
for(int count=0;count<10;count++){
if(collector.get(count).threadTotal == null){
Thread.sleep(100);
break;
}
}
allCollected = true;
}
// sum totals of all threads
for(int count=0;count<10;count++){
totalSum += collector.get(count).threadTotal;
}