• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

OCP Exam - how to identify compilation error lines

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This simple test come from "Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815", chapter II, test 18.

The following response are valid:

F) Line 5 contains a compiler error
G) Line 9 contains a compiler error

My problem is that javac reports only "F", and reports "G" only after "F" is fixed.

With this "after fixing" behavior, the code block can also print
 
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see why try to use a just declared primitive should cause a compile time error at all. As far as I'm aware primitives are always initialized to their default, for int this is 0. So it should compile and executed by printing 0.
 
Marshal
Posts: 72059
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Matthew Bendford wrote:. . . a just declared primitive should cause a compile time error . . . primitives are always initialized to their default . . .

That only applies to fields, array elements, etc. If we are using var, we must be in the context of local variables, and local variables must be definitely assigned to before use. They don't have default values. Line 9 must therefore throw a compile time error. I am a bit surprised that we didn't see both compiler errors together.
 
Ray Whittle
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:...I am a bit surprised that we didn't see both compiler errors together.



My problem is how this type of questions are to be considered:

A) Find what's wrong in the code.
Line 5 and 9 are problematic.

B) What do you get if you run this code?
Line 5 cause java compilation error

It's non clear.

Unfortunately, partial responses  are evaluated as 0!
 
Marshal
Posts: 26381
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Bendford wrote:As far as I'm aware primitives are always initialized to their default, for int this is 0. So it should compile and executed by printing 0.



Not exactly: declaring a local variable doesn't include automatic initialization like that. It only applies to static variables and instance variables. Unfortunately the posted code doesn't make it clear whether it's inside a method, which would mean that "mouse" is a local variable and hence not initialized.
 
Campbell Ritchie
Marshal
Posts: 72059
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . the posted code doesn't make it clear whether it's inside a method . . .

You have to infer that from the use of var in line 3. You can only use var on local variables ∴ the following line is also a local variables declaration.
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic