Java - Quiz Ganesh Samarthyam ganesh@codeops.tech
Programmer’s world :) java.lang.NullPointerException at org.eclipse.jface.dialogs.DialogSettings.load(DialogSettings.java:278) at org.eclipse.jface.dialogs.DialogSettings.load(DialogSettings.java:306) at org.eclipse.jface.dialogs.DialogSettings.load(DialogSettings.java:247) at org.eclipse.jface.dialogs.DialogSettings.load(DialogSettings.java:264) at org.eclipse.ui.plugin.AbstractUIPlugin.loadDialogSettings(AbstractUIPlugin.java:411) at org.eclipse.ui.plugin.AbstractUIPlugin.getDialogSettings(AbstractUIPlugin.java:232) at org.eclipse.jdt.internal.ui.JavaPlugin.getDialogSettingsSection(JavaPlugin.java:1012) at org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart.<init>(PackageExplorerPart.java:436) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance0(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.createExecutableExtension(RegistryStrategyOSGI.java:170) at org.eclipse.core.internal.registry.ExtensionRegistry.createExecutableExtension(ExtensionRegistry.java:867) at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:243) at org.eclipse.core.internal.registry.ConfigurationElementHandle.createExecutableExtension(ConfigurationElementHandle.java:51) at org.eclipse.ui.internal.WorkbenchPlugin$1.run(WorkbenchPlugin.java:267) at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70) at org.eclipse.ui.internal.WorkbenchPlugin.createExtension(WorkbenchPlugin.java:263) at org.eclipse.ui.internal.registry.ViewDescriptor.createView(ViewDescriptor.java:63) at org.eclipse.ui.internal.ViewReference.createPartHelper(ViewReference.java:328) at org.eclipse.ui.internal.ViewReference.createPart(ViewReference.java:230) at org.eclipse.ui.internal.WorkbenchPartReference.getPart(WorkbenchPartReference.java:594) at org.eclipse.ui.internal.Perspective.showFastView(Perspective.java:2053) at org.eclipse.ui.internal.Perspective.setActiveFastView(Perspective.java:1835) at org.eclipse.ui.internal.Perspective.setActiveFastView(Perspective.java:1848) at org.eclipse.ui.internal.Perspective.toggleFastView(Perspective.java:2241) at org.eclipse.ui.internal.WorkbenchPage.toggleFastView(WorkbenchPage.java:3824) at org.eclipse.ui.internal.ShowFastViewContribution.showView(ShowFastViewContribution.java:157) at org.eclipse.ui.internal.ShowFastViewContribution.access$1(ShowFastViewContribution.java:155) at org.eclipse.ui.internal.ShowFastViewContribution$3.widgetSelected(ShowFastViewContribution.java:138) at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:228) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1003) at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3823) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3422) at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2382) at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2346) at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2198) at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:493) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:288) at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:488) at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149) at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:113) at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:193) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:382) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:549) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:504) at org.eclipse.equinox.launcher.Main.run(Main.java:1236) SNAFU!
Let’s have some fun!
Shout out your answer
Which browser does this code open? a) Internet Explorer b) Opera c) FireFox d) Safari e) The default browser in your machine class Google { public static void main(String []args) { http://www.google.com System.out.println("Hello world"); } }
int i = (false) ? 1 : null; System.out.println(i);
What does this program print? class MyThread implements Runnable { public void run() { System.out.println("In run method; thread name is: " + Thread.currentThread().getName()); } public static void main(String args[]) throws Exception { Thread myThread = new Thread(new MyThread()); myThread.run(); System.out.println("In main method; thread name is : " + Thread.currentThread().getName()); } }
Answer In run method; thread name is: main In main method; thread name is : main
What does this program print? class CallThread extends Thread { public void run() { System.out.println("In CallThread.run()"); } public static void main(String []s) { Thread thread = new CallThread(); thread.start(); thread.start(); } }
Answer In CallThread.run() Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:705) at CallThread.main(CallThread.java:9) Never call the start() method twice on the same thread.
Determine the output/behaviour of this program List<String> aList = new ArrayList<>(); aList.add("one"); aList.add("two"); aList.add("three"); Iterator listIter = aList.iterator(); while(listIter.hasNext()) { System.out.println(listIter.next()); aList.add("four"); }
Answer This program throws java.util.ConcurrentModificationException Because the iterators of ArrayList are fail-fast; it fails by throwing ConcurrentModificationException if it detects that the underlying container has changed when it is iterating over the elements in the container. This behavior is useful in concurrent contexts when one thread modifies the underlying container when another thread is iterating over the elements of the container. You can use CopyOnWriteArrayList for making such changes to the underlying container when iteration is happening.
What is the output of this program? class Max { public static void main(String []args) { int i = ~0; System.out.println(i); } } A. This program prints the value of Integer.MAX_VALUE B. This program prints the value of Integer.MIN_VALUE C. This program prints “0” D. This program prints “-1”
Determine the behaviour of this program: class Bitwise { public static void main(String []arg) { Integer i = null; if((i != null) & (i.intValue() != 0)) { System.out.println(i); } } } A. This program prints 0 to the console B. This program does not print anything output to the console and terminates C. This program crashes after throwing a NullPointerException D. This program prints null to the console
class ReplaceAll { public static void main(String []args) { String date = "18-03-2017" ; String date1 = date.replaceAll( "-" , "." ) ; String date2 = date1.replaceAll( "." , "/" ) ; System.out.println(date2); } } What is the output of this program? a) 18-03-2017 b) ---------- c) ………. d) //////////
Supplier<String> s = () -> "hello world"; System.out.println(s.get()); What bytecode instruction would s.get() generate?
class Vehicle { public static void run() { System.out.println("In Vehicle.run()"); } public static void main(String[] args) { Vehicle vehicle = null; vehicle.run(); } } class Car { public static void run() { System.out.println("In Car.run()"); } }
reachus@codeops.tech www.codeops.tech @CodeOpsTech +91 98801 64463

Java Quiz - Meetup

  • 1.
    Java - Quiz GaneshSamarthyam ganesh@codeops.tech
  • 2.
    Programmer’s world :) java.lang.NullPointerException atorg.eclipse.jface.dialogs.DialogSettings.load(DialogSettings.java:278) at org.eclipse.jface.dialogs.DialogSettings.load(DialogSettings.java:306) at org.eclipse.jface.dialogs.DialogSettings.load(DialogSettings.java:247) at org.eclipse.jface.dialogs.DialogSettings.load(DialogSettings.java:264) at org.eclipse.ui.plugin.AbstractUIPlugin.loadDialogSettings(AbstractUIPlugin.java:411) at org.eclipse.ui.plugin.AbstractUIPlugin.getDialogSettings(AbstractUIPlugin.java:232) at org.eclipse.jdt.internal.ui.JavaPlugin.getDialogSettingsSection(JavaPlugin.java:1012) at org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart.<init>(PackageExplorerPart.java:436) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance0(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.createExecutableExtension(RegistryStrategyOSGI.java:170) at org.eclipse.core.internal.registry.ExtensionRegistry.createExecutableExtension(ExtensionRegistry.java:867) at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:243) at org.eclipse.core.internal.registry.ConfigurationElementHandle.createExecutableExtension(ConfigurationElementHandle.java:51) at org.eclipse.ui.internal.WorkbenchPlugin$1.run(WorkbenchPlugin.java:267) at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70) at org.eclipse.ui.internal.WorkbenchPlugin.createExtension(WorkbenchPlugin.java:263) at org.eclipse.ui.internal.registry.ViewDescriptor.createView(ViewDescriptor.java:63) at org.eclipse.ui.internal.ViewReference.createPartHelper(ViewReference.java:328) at org.eclipse.ui.internal.ViewReference.createPart(ViewReference.java:230) at org.eclipse.ui.internal.WorkbenchPartReference.getPart(WorkbenchPartReference.java:594) at org.eclipse.ui.internal.Perspective.showFastView(Perspective.java:2053) at org.eclipse.ui.internal.Perspective.setActiveFastView(Perspective.java:1835) at org.eclipse.ui.internal.Perspective.setActiveFastView(Perspective.java:1848) at org.eclipse.ui.internal.Perspective.toggleFastView(Perspective.java:2241) at org.eclipse.ui.internal.WorkbenchPage.toggleFastView(WorkbenchPage.java:3824) at org.eclipse.ui.internal.ShowFastViewContribution.showView(ShowFastViewContribution.java:157) at org.eclipse.ui.internal.ShowFastViewContribution.access$1(ShowFastViewContribution.java:155) at org.eclipse.ui.internal.ShowFastViewContribution$3.widgetSelected(ShowFastViewContribution.java:138) at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:228) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1003) at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3823) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3422) at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2382) at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2346) at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2198) at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:493) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:288) at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:488) at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149) at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:113) at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:193) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:382) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:549) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:504) at org.eclipse.equinox.launcher.Main.run(Main.java:1236) SNAFU!
  • 3.
  • 4.
  • 6.
    Which browser doesthis code open? a) Internet Explorer b) Opera c) FireFox d) Safari e) The default browser in your machine class Google { public static void main(String []args) { http://www.google.com System.out.println("Hello world"); } }
  • 7.
    int i =(false) ? 1 : null; System.out.println(i);
  • 8.
    What does thisprogram print? class MyThread implements Runnable { public void run() { System.out.println("In run method; thread name is: " + Thread.currentThread().getName()); } public static void main(String args[]) throws Exception { Thread myThread = new Thread(new MyThread()); myThread.run(); System.out.println("In main method; thread name is : " + Thread.currentThread().getName()); } }
  • 9.
    Answer In run method;thread name is: main In main method; thread name is : main
  • 10.
    What does thisprogram print? class CallThread extends Thread { public void run() { System.out.println("In CallThread.run()"); } public static void main(String []s) { Thread thread = new CallThread(); thread.start(); thread.start(); } }
  • 11.
    Answer In CallThread.run() Exception inthread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:705) at CallThread.main(CallThread.java:9) Never call the start() method twice on the same thread.
  • 12.
    Determine the output/behaviourof this program List<String> aList = new ArrayList<>(); aList.add("one"); aList.add("two"); aList.add("three"); Iterator listIter = aList.iterator(); while(listIter.hasNext()) { System.out.println(listIter.next()); aList.add("four"); }
  • 13.
    Answer This program throwsjava.util.ConcurrentModificationException Because the iterators of ArrayList are fail-fast; it fails by throwing ConcurrentModificationException if it detects that the underlying container has changed when it is iterating over the elements in the container. This behavior is useful in concurrent contexts when one thread modifies the underlying container when another thread is iterating over the elements of the container. You can use CopyOnWriteArrayList for making such changes to the underlying container when iteration is happening.
  • 14.
    What is theoutput of this program? class Max { public static void main(String []args) { int i = ~0; System.out.println(i); } } A. This program prints the value of Integer.MAX_VALUE B. This program prints the value of Integer.MIN_VALUE C. This program prints “0” D. This program prints “-1”
  • 15.
    Determine the behaviourof this program: class Bitwise { public static void main(String []arg) { Integer i = null; if((i != null) & (i.intValue() != 0)) { System.out.println(i); } } } A. This program prints 0 to the console B. This program does not print anything output to the console and terminates C. This program crashes after throwing a NullPointerException D. This program prints null to the console
  • 16.
    class ReplaceAll { publicstatic void main(String []args) { String date = "18-03-2017" ; String date1 = date.replaceAll( "-" , "." ) ; String date2 = date1.replaceAll( "." , "/" ) ; System.out.println(date2); } } What is the output of this program? a) 18-03-2017 b) ---------- c) ………. d) //////////
  • 17.
    Supplier<String> s =() -> "hello world"; System.out.println(s.get()); What bytecode instruction would s.get() generate?
  • 18.
    class Vehicle { publicstatic void run() { System.out.println("In Vehicle.run()"); } public static void main(String[] args) { Vehicle vehicle = null; vehicle.run(); } } class Car { public static void run() { System.out.println("In Car.run()"); } }
  • 19.